I have a form whose action is set to itself. I want it this way so that is returns previously entered values if you have already submitted the form once (with an error), so that you don’t have to completely redo the form. I have:
<?php
if (isset($_POST['submit'])) {
$firstname= $_POST['firstname'];
$lastname= $_POST['lastname'];
$output_form= false;
if ($firstname== "empty($firstname) && empty($lastname)) {
echo "you forgot you first and last names!!";
$output_form= true;
}
if (!empty($firstname) && empty($lastname)) {
echo "you forgot your last name.";
$output_form= true;
}
if (empty($firstname) && !empty($lastname)) {
echo "you forgot your firstname.";
$output_form= true;
}
if (!empty($firstname) && !empty($lastname)) {
echo "good job!!";
}
}
else
{
$output_form= true;
}
if ($output_form) {
?>
<form method="post" action=" <?php echo $_SERVER['PHP_SELF']; ?>">
First Name: <input type="text" name="firstname" value="<?php echo $firstname ?>"/>
Last Name: <input type="text" name="lastname" value="<?php echo $lastname ?>"/>
<input type="submit" name="submit" value="submit"/>
</form>
<?php
}
?>
If the form has not yet been submitted the $output_form variable will return true in my if statement. But I get an error because the variables $firstname and $lastname are not yet defined (because the isset($_POST['submit'] returns a false value; the form has not yet been submitted) What can I do to keep my form “sticky”?
set default values for your variables