PHP Form check

How do I check if all the textboxes in a form are filled out. I have managed to get the userName checked but can’t figure out how to add the other textboxes.

<?php $chkError = FALSE; $userName = filter_input(INPUT_GET, "userName"); $address = filter_input(INPUT_GET, "address"); $city = filter_input(INPUT_GET, "city"); $state = filter_input(INPUT_GET, "state"); $zip = filter_input(INPUT_GET, "zip"); if(!isset($_GET['userName']) || $userName !== '') { $chkError = TRUE; print "Hi there, $userName. I see you live at $address $city $state $zip"; } else { $chkError = FALSE; print "You did not enter your name. Please hit the back button on your browser and reenter the information."; } ?>

Thanks everyone!

Depends on how complicated you want to get. If you just want 1 message, then just check to see if $_POST is empty (if(empty($_POST)) {…}). If you want a message for each input, then just keep going like u have it now.

Your hello message is in the wrong place, it should where you have the error message.

I don’t know what filter_input does, but it looks like its doing the same thing as the php messages.
[php]<?php
$chkError = FALSE;
$userName = filter_input(INPUT_GET, “userName”);
$address = filter_input(INPUT_GET, “address”);
$city = filter_input(INPUT_GET, “city”);
$state = filter_input(INPUT_GET, “state”);
$zip = filter_input(INPUT_GET, “zip”);

if(!isset($_GET[‘userName’]) || $userName !== ‘’) {
$chkError = TRUE;
echo “You did not enter your name. Please hit the back button on your browser and reenter the information.”;
} else {
$chkError = FALSE;
echo “Hi there, $userName. I see you live at $address $city $state $zip”;
}
?>[/php]

See Below

This has all the textboxes included. It is only printing the error message. This is with the error message corrected. Thanks for your help!

<?php $chkError = FALSE; $userName = filter_input(INPUT_POST, "userName"); $address = filter_input(INPUT_POST, "address"); $city = filter_input(INPUT_POST, "city"); $state = filter_input(INPUT_POST, "state"); $zip = filter_input(INPUT_POST, "zip"); if(!isset($_POST['userName']) || $userName !== '') { $chkError = TRUE; print "You did not fill in your name. Please hit the back button on your browser and reenter the information."; } else if(!isset($_POST['address']) || $address !== '') { $chkError = TRUE; print "You did not fill in your address. Please hit the back button on your browser and reenter the information."; } else if(!isset($_POST['city']) || $city !== '') { $chkError = TRUE; print "You did not fill in your city. Please hit the back button on your browser and reenter the information."; } else if(!isset($_POST['state']) || $state !== '') { $chkError = TRUE; print "You did not fill in your state. Please hit the back button on your browser and reenter the information."; } else if(!isset($_POST['zip']) || $zip !== '') { $chkError = TRUE; print "You did not fill in your zip code. Please hit the back button on your browser and reenter the information."; } else { $chkError = FALSE; print "Hi there, $userName. I see you live at $address $city $state $zip"; } //end if} ?>

put
echo “

”;
print_r($_POST);
echo “
”;
before the variables. that will tell u if any data is getting through.

This is what it prints:

"Array
(
)

Hi there, Jane Doe. I see you live at 555 Sesame street way Somewhere New York 55555"

If I take out something from one of the text boxes it does not print error message either. It tries to print the above message but without the text box I removed text from to test it.

Do you have this php bit looking for the submit button? if not, try this code out
[php]<?php
if(isset($_POST[‘submit’])) { // change submit to whatever you named ur button
$chkError = FALSE;

echo “

”;
print_r($_POST);
echo “
”;

if(empty($_POST[‘userName’])) {
$chkError = TRUE;
echo “You did not fill in your name. Please hit the back button on your browser and reenter the information.”;
} else {
$chkError = FALSE;
$userName = mysql_real_escape_string($_POST[‘userName’]);
}
if(empty($_POST[‘address’])) {
$chkError = TRUE;
echo “You did not fill in your address. Please hit the back button on your browser and reenter the information.”;
} else {
$chkError = FALSE;
$address = mysql_real_escape_string($_POST[‘address’]);
}
if(empty($_POST[‘city’])) {
$chkError = TRUE;
echo “You did not fill in your city. Please hit the back button on your browser and reenter the information.”;
} else {
$chkError = FALSE;
$city = mysql_real_escape_string($_POST[‘city’]);
}
if(empty($_POST[‘state’])) {
$chkError = TRUE;
echo “You did not fill in your state. Please hit the back button on your browser and reenter the information.”;
} else {
$chkError = FALSE;
$state = mysql_real_escape_string($_POST[‘state’]);
}
if(empty($_POST[‘zip’])) {
$chkError = TRUE;
echo “You did not fill in your zip code. Please hit the back button on your browser and reenter the information.”;
} else {
$chkError = FALSE;
$zip = mysql_real_escape_string($_POST[‘zip’]);
}
if($chkError == FALSE) {
echo “Hi there, $userName. I see you live at $address, $city, $state $zip”;
}
}
?>[/php]
empty() does the same thing as what you had. It can’t be set (isset()) if its empty, so looking for it is rather redundant.

I don’t know what your form looks like, but each of the fields need to have a name (name = “userName”), else it won’t find it. I took out filter_input() because it wasn’t doing anything, you weren’t telling it what to filter, if you still want to use it, i’d suggest filter_input_array(), then you can just use it on $_POST through a loop in a function.

I got it! Thank you for your help. :smiley:

Sponsor our Newsletter | Privacy Policy | Terms of Service