Redisplaying Form Values

When a PHP form page is submitted, all of the form field values sent to the PHP script through the form's get or post method. When the form page is redisplayed, all of the form fields are blank regardless of whether the values are valid. This happens because a new instance of the PHP page has been retrieved by the server and sent to the browser. However, it would be convenient for the user if, when the form is redisplayed, its fields were populated with the information previously submitted. In this way the user would only need to correct the data in error rather than having to reenter values in all the fields. This is not a particular problem with a form containing only two fields. However, it would be a great inconvenience with a form containing a large number of fields.

Enter values into the following form page and click the submit button. When the form is submitted all form fields are blank.

Enter First Name: 
Enter Last Name:

When the form page is submitted and a new instance of the PHP page is retrieved by the server and sent to the browser, the values of the form field values can be dynamically updated so that the fields are not returned blank. The page below demonstrates this technique:

Enter First Name: 
Enter Last Name:

When this page is submitted, all form fields still contain the original data entered by the user. Now, it is more convenient for the user to simply update those fields that contain errors and not every form field.

Recall that when a form is submitted, the form field values are stored in the $_POST or $_GET superglobal variables. In the previous example, the user's first name is stored in the array variable $_POST['FName'] and the last name is stored in the array variable $_POST['LName']. Thus, redisplaying the form field values can be accomplished by assigning the form field 'value' attributes with the PHP code to display the values contained in the array variables. The following code demonstrates this technique.

process.php



<!DOCTYPE html PUBLIC "-//W3C//DTD/XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml11-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>A Web Page</title>
</head>
<body>

<form action="process.php" method="post">

First Name: <input type="text" name="FName" value="<?php echo $_POST['FName']?>"/>
Last Name: <input type="text" name="LName" value="<?php echo $_POST['LName']?>"/>


<input type="submit" name="submit" value="Submit Data"/>

</form>

</body>
</html>

Notice that the values of the First Name and Last Name text boxes have been updated to include the following PHP code:

<?php echo $_POST['FName']?>
<?php echo $_POST['LName']?>

When this code is assigned to the value attribute, it redisplays the values previously entered by the user. When the page loads for the first time, the values of the POST variables are NULL and value = "". The value attribute contains a value only after the submit button has been clicked.

Redisplaying form field values is a useful technique that will be used extensively in upcoming sections.