A: 

You'll have to check the data within the same file, and if it is correct, then you redirect to the correct location. Then you can use the $_POST or $_GET information the user posted and he can fix the error(s).

Ólafur Waage
+2  A: 

You need to do this yourself. When the page gets posted you'll have access to all the form values the user entered via $POST['...']. You can then re-populate the form fields with this data.

Kevin Tighe
+1  A: 

This is not done automatically. They get values from post / get and then assign the values the user typed to the template. What you see is html that was generated from the script that handled user values.

empi
A: 

You can use two approachs (they're not mutually exclusive):

  • Use JavaScript to help the user before he submits the form. That way, you save a roundtrip to the server. What you asked for:
  • In the form, fill the value attributes of the fields with the data sent back from the server. For example: you send a field name, which you get as $_POST['name'] in PHP (assuming you used method='post'. If you send back the data and modify that field adding value='<?php $_POST['name']; ?> you should get your data back.
Adriano Varoli Piazza
+1  A: 

If you put your form and the form data processing in the same script, you can easily print out the data that has already been entered in the form, e.g.:

$valid = false;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['name']) && $_POST['name'] == 'Hugo') {
        $valid = true;
    } else {
        echo '<p>Seriously, you have to enter "Hugo"!</p>';
    }
    // more data processing
    if ($valid) {
        echo '<p>Everything’s fine!</p>';
    }
}
if (!$valid) {
    echo '<form action="" method="post">';
    echo '<p>Please enter "Hugo": <input type="text" name="name" value="', (isset($_POST['name']) ? htmlspecialchars($_POST['name']) : ''), '"></p>';
    echo '<p><input type="submit"></p>';
    echo '</form>';
}

Well this is not nice example but that’s how it works.

Gumbo
+1  A: 

a lot of frameworks do this job for you, so dont waste your time doing this manually

Rafael Mueller
+1  A: 

Here is a modified version of what I use for very simple websites where I don't want/need an entire framework to get the job done.

function input($name, $options = array()) {
 if(!isset($options['type'])) $options['type'] = 'text';

 $options['name'] = $name;

 if(isset($_POST[$name]) && $options['type'] != 'password') {
  $options['value'] = htmlspecialchars($_POST[$name]);
 }

 $opts = array(); 
 foreach($options as $key => $value) {
  $opts[] = $key . '="' . $value . '"';
 }

 return '<input ' . implode(' ', $opts) . '/>';
}

(I have a few similar functions for <select> and <textarea> and so on)

When you're building fields you can do something like:

First Name: <?=input('first_name')?>
Last Name: <?=input('last_name')?>
Password: <?=input('password', array('type' => 'password'))?>

If you process your forms in the same page as the form itself, they will get auto filled if there are any errors. Most frameworks, though, do all of this for you (and in a much better way than the code above), I personally suggest CakePHP or CodeIgniter.

Paolo Bergantino
A: 

If you're using a template or framework system (I've incorporated the Smarty engine into several projects of mine), you can usually tweak the templates so they automatically fill fields with values if they detect that the $_POST[$variable] value corresponding to their field is set.

As for the passwords, as far as I understand it (I could be wrong): it's a convention that minimizes the amount of time that password is being sent over the wire, hence shrinking the window for anyone who may be sniffing to pick up on the text. It's just good practice to leave password fields blank, is all.

Magsol