Reputation: 783
I've made a form where only 5 fields are required, other are optional.
My form action is empty but the onSubmit is this:
function validateInput()
{
var ifEmpty = (document.forms['orderform'].naam.value == "" ||
document.forms['orderform'].woonplaats.value == "" ||
document.forms['orderform'].straatnaam.value == "" ||
document.forms['orderform'].huisnummer.value == "" ||
document.forms['orderform'].telefoonnummer.value == "");
if(ifEmpty)
{
document.orderform.action ="index.php?page=pizzaorder";
}else{
document.orderform.action ="index.php?page=pizza_bestelling";
}
}
So whenever 1 of those 5 fields is empty the action will be index.php?page=pizzaorder
if all is well the action will be index.php?page=pizza_bestelling
this is all working just as it should. However if one of those fields is empty, it will submit to itself which is page=pizzaorder.
(the reason i can't use $_SERVER['PHP_SELF']
is because i'm using an include menu. Which means i am always on the same page, it will just include different files based on menu options. So if i were to use $_SERVER['PHP_SELF']
i would be send to the default index.php which would be my homepage, which isn't what i want)
Anyway, if one of those fields isn't filled it will send to the same page with the error messages next to the field that is empty, which is working fine as well.:
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["naam"]))
{$naamErr = "Vul uw naam in";}
else
{$naam = test_name($_POST["naam"]);}
if (empty($_POST["woonplaats"]))
{$woonplaatsErr = "Vul uw Woonplaats in";}
else
{$woonplaats = test_name($_POST["woonplaats"]);}
if (empty($_POST["straatnaam"]))
{$straatErr = "Vul uw straat in";}
else
{$straatnaam = test_name($_POST["straatnaam"]);}
if (empty($_POST["huisnummer"]))
{$huisnummerErr = "Vul uw Huisnummer in";}
else
{$huisnummer = test_name($_POST["huisnummer"]);}
if (empty($_POST["telefoonnummer"]))
{$telefoonErr = "Vul uw Telefoonnummer in";}
else
{$telefoonnummer = test_name($_POST["telefoonnummer"]);}
}
However all fields are empty if this happens, which is NOT what i want. If the user fills in correctly all his info but forgets his, let's say, street he only sees the error message Street is required, but all his other data is empty as well. Only difference is there is no error message.
So what i want is that if he gets sent back to this page with the error messages, the other info shouldn't be empty as well.
I hope it's somewhat clear what my problem is, otherwise feel free to ask for clarification!
Thanks in advance!
Upvotes: 0
Views: 548
Reputation: 5488
So basically, whenever a user is shown the form to be re-entered, you need it to show the value that he entered previously.These are called sticky forms. So, in each input tag of the form, against the value, check if the $_POST super global is not empty and echo it in it's value attribute.
<input type="text" name="telefoonnummer" value="<?php echo (isset($_POST['telefoonnummer']) && !empty($_POST['telefoonnummer']))?$_POST['telefoonnummer']:'';?>"/>
For readability -
<input type="text" name="telefoonnummer" value="
<?php
echo (isset($_POST['telefoonnummer']) && !empty($_POST['telefoonnummer']))?$_POST['telefoonnummer']:'';
?>
"/>
Upvotes: 1