•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 426,793 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,763 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 5239 | Replies: 6
![]() |
•
•
Join Date: Jan 2005
Posts: 13
Reputation:
Rep Power: 4
Solved Threads: 0
The form (form.htm) posts a value from a text box (id='first_name') into a script (test.php) that checks the data entered. The script takes an array of required fields from a config file (config.php) and uses these to validate the data that comes from form.htm.
If the required fields ($required) are not filled in then an error message is displayed. If they are then a 'Thank You' message is displayed.
My problem is with the following script:
Regardless of what is entered on the form, the error message is displayed. However if i replace if(!$_POST[$field]) with if(!$_POST['first_name'])...which is the name of the required form field...the script works perfectly.
Is there a problem with the way I am trying to access the $field variable with $_POST?
Sorry if its unclear :o
Thanks for your help,
JameZ
If the required fields ($required) are not filled in then an error message is displayed. If they are then a 'Thank You' message is displayed.
My problem is with the following script:
<?php
require_once("config.php");
if ($required)
{
$require = explode(",",$required);
for($n=0; $n<count($require); $n++);
{
$field = $require[$n];
if(!$_POST[$field])
{
print $msg['required'];
}
else
{
print 'Thank You';
}
}
}
?>Regardless of what is entered on the form, the error message is displayed. However if i replace if(!$_POST[$field]) with if(!$_POST['first_name'])...which is the name of the required form field...the script works perfectly.
Is there a problem with the way I am trying to access the $field variable with $_POST?
Sorry if its unclear :o
Thanks for your help,
JameZ
•
•
Join Date: Sep 2004
Location: in the j00-kay
Posts: 114
Reputation:
Rep Power: 5
Solved Threads: 5
With the If statement are you trying to compare the posted values with the variables in the config.php array ?
if so, you should try ask if the posted values are the same as those static values in the config.php array.
i.e.
[PHP]
if(!$_POST['first_name'] == $field) {
{
print $msg['required'];
}
else
{
print 'Thank You';
}
[/PHP]
if so, you should try ask if the posted values are the same as those static values in the config.php array.
i.e.
[PHP]
if(!$_POST['first_name'] == $field) {
{
print $msg['required'];
}
else
{
print 'Thank You';
}
[/PHP]
I'm pink, therefore, im spam.
http://www.vivaci.net - Quality Webhosting
http://gaming.vivaci.net - FAST UK Gaming servers
http://www.getsigned.org - Free Image Uploads
http://www.vivaci.net - Quality Webhosting
http://gaming.vivaci.net - FAST UK Gaming servers
http://www.getsigned.org - Free Image Uploads
•
•
Join Date: Jan 2005
Posts: 13
Reputation:
Rep Power: 4
Solved Threads: 0
ReDuX:
Yeah thats exactly what i want to do but the code you provided will only check a specifically defined field....i.e. the first_name that you put in the code.
That works but its not exactly what i need - I need the script to access the config.php file to get the required fields.....its all about ease of use for the end-user.
Basically I want the required fields ONLY to be defined in the config.php file. It just will make it easier...if i decide to distribute the script...for the user to say what required fields they want to add - if ya get me
iceboxman:
Thanks for your suggestion - Welcome to the forum by the way
The thing about your code example is it will only check to see if there are values in the $_POST variable. Plus $_POST is a replacement for $HTTP_POST_VARS so the second part is not needed
Thanks for all your support though....any other ideas?
JameZ
Yeah thats exactly what i want to do but the code you provided will only check a specifically defined field....i.e. the first_name that you put in the code.
That works but its not exactly what i need - I need the script to access the config.php file to get the required fields.....its all about ease of use for the end-user.
Basically I want the required fields ONLY to be defined in the config.php file. It just will make it easier...if i decide to distribute the script...for the user to say what required fields they want to add - if ya get me

iceboxman:
Thanks for your suggestion - Welcome to the forum by the way

The thing about your code example is it will only check to see if there are values in the $_POST variable. Plus $_POST is a replacement for $HTTP_POST_VARS so the second part is not needed

Thanks for all your support though....any other ideas?
JameZ
•
•
•
•
Originally Posted by nikez
ReDuX:
iceboxman:
Thanks for your suggestion - Welcome to the forum by the way
The thing about your code example is it will only check to see if there are values in the $_POST variable. Plus $_POST is a replacement for $HTTP_POST_VARS so the second part is not needed
Thanks for all your support though....any other ideas?
JameZ
Thansk for welcoming me. The reason I provided that code is because if your PHP version is < 4.1.0 then $_POST will not hold anything. you could do a print_r($_POST) to see what exactly is held in $_POST.
I find having it show you the contents of your variables is one of the best ways to find a starting point of where to troubleshoot. Just looking at the code, however, it looks sound assuming you set $required = 'first_name' in the config file.
•
•
Join Date: Jan 2005
Posts: 13
Reputation:
Rep Power: 4
Solved Threads: 0
hehehe yeah never thought about it like that. Yeah print_r() is very useful for displaying what a variable holds - especially arrays and the $_POST variable.
I managed to fix the code and it all works perfectly now. If anyone is interested then the code is:
[PHP]if($ValidData['Required'])
{
foreach(explode(",",$ValidData['Required']) as $field)
{
if(empty($_POST[$field]))
{
$field = str_replace("_"," ",$field);
$field = ucwords($field);
$empty_fields = "<li><b><font color=\"".$Colour['Required']."\">$field </font></b></li>";
if($empty_fields)
print $empty_fields .$ErrorMsg['Required'];
}
}
}[/PHP]
Thanks for all your support guys
JameZ
I managed to fix the code and it all works perfectly now. If anyone is interested then the code is:
[PHP]if($ValidData['Required'])
{
foreach(explode(",",$ValidData['Required']) as $field)
{
if(empty($_POST[$field]))
{
$field = str_replace("_"," ",$field);
$field = ucwords($field);
$empty_fields = "<li><b><font color=\"".$Colour['Required']."\">$field </font></b></li>";
if($empty_fields)
print $empty_fields .$ErrorMsg['Required'];
}
}
}[/PHP]
Thanks for all your support guys

JameZ
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- paging & _POST variables (PHP)
- bar graph & _POST variables (PHP)
Other Threads in the PHP Forum
- Previous Thread: How do I add this to that to get this?
- Next Thread: Validating Email Addresses - A little extra help!


Linear Mode