Search code examples
phpvalidationheaderhidden-field

Using header in PHP with hidden input fields


I am creating a validation function in a php-file for my HTML FORM INPUT fields. The function should jump back to the form if it is not filled out correctly and jump to a new page if it is filled out correctly. How do I send the information from the form without having to write the information in the URL by using header?

The form in index.php:

<form method='post' action='validate.php'>
Name: <input type='text' name='name'>
Job: <input type='text' name='job'>
City: <input type='text' name='city'>
<input type='submit' value='Submit'>
</form>

Validate.php:

if (!empty($_REQUEST['name']) And (!empty($_REQUEST['job'])) And (!empty($_REQUEST['city']))){
$name = $_REQUEST['name'];
$job = $_REQUEST['job'];
$city = $_REQUEST['city'];
header("Location: go.php?name=$name&&job=$job&&city=$city");
}

else {
header("Location: index.php");
}

I would also like the jump back to index.php to have the already filled in information if it is only one field that has not been filled out.


Solution

  • You shouldn't do that.
    Don't jump anywhere.

    • Make index.php an action for the form
    • in that file detect if the form was sent and include Validate.php
    • if validation failed just show your form again and fill all the fields (don't forget htmlspecialchars())

    HTML standard require every form value (as well as textarea content)to be encoded using htmlspecialchars()