php form validation..

I am trying to create a form that lets the user know if they didnt enter information when they hit the submit button.

[code]

Assignment2

What's your name?

Writing a form for user input

Please type your first name:
Please type your last name:
Please type your city:
Please type your State:
Please type your Zipcode:
[/code] [php] Assignment2

Personal Information

<?php $chkErrors = FALSE; if (isset($_POST['fName'])){ echo " please hit the back arrow and enter your name "; $chkErrors= TRUE; } if (isset($_POST['lName'])){ echo " please hit the back arrow and enter your Last Name "; $chkErrors = TRUE; } if (isset($_POST['city'])){ echo " please hit the back arrow and enter your city"; $chkErrors = TRUE; } if (isset($_POST['state'])){ echo " please hit the back arrow and enter your state"; $chkErrors= TRUE; } if (isset($_POST['zipCode'])){ echo " please hit the back arrow and enter your zipcode "; $chkErrors= TRUE; } $fName = filter_input(INPUT_POST, "fName"); $lName = filter_input(INPUT_POST, "lName"); $city = filter_input(INPUT_POST, "city"); $state = filter_input(INPUT_POST, "state"); $zipCode = filter_input(INPUT_POST, "zipCode"); if($chkErrors == FALSE) { echo <<<HERE

Hi there, $fName $lName,
so you live in $city,
in the great state of $state.
I hear the climate around $zipCode is great this time of year.
$fName, I hear that $state has alot to offer as far as recreation goes.
I hope that you have a great summer in $city.

HERE; } ?> [/php]

You should add an exclamation point before the isset function like so:

From:
[php]
// Where you are testing for if the array element fName is set
if (isset($_POST[‘fName’])){
echo " please hit the back arrow and enter your name ";
$chkErrors= TRUE;
}
[/php]

To:
[php]
// Testing if the array element fName is NOT set
if (!isset($_POST[‘fName’])){
echo " please hit the back arrow and enter your name ";
$chkErrors= TRUE;
}
[/php]

If any fields from the form are blank, they will remain blank when they enter the $_POST array and therefore will be essentially uninitialized (not set). So you want to test if these are not set.

Sponsor our Newsletter | Privacy Policy | Terms of Service