Web Development Tutorials




  
  

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 re-enter 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.

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. A form that remembers user input is known as a sticky form. The page below demonstrates this technique:

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

   Enter First Name: <input name="FName" type="text" /><br />

   Enter Last Name: <input name="LName" type="text" /><br />

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

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, $_GET, or $_REQUEST superglobal variables. In the previous example, the user's first name is stored in the array variable $_REQUEST['FName'] and the last name is stored in the array variable $_REQUEST['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>

<html>
<head>
  <title>A Web Page</title>
</head>
<body>

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

First Name: <input type="text" name="FName" value="<?php echo $_REQUEST['FName']?>">
Last Name: <input type="text" name="LName" value="<?php echo $_REQUEST['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 $_REQUEST['FName']?>
<?php echo $_REQUEST['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 REQUEST variables are NULL and value = "". The value attribute contains a value only after the submit button has been clicked.

The code above works properly if the REQUEST variables contain values. When the page loads for the first time or prior to a form submission, the REQUEST variables will be undefined. This will lead to the following errors being displayed within the text box controls upon page load:

Notice:  Undefined index: FName in C:\xampp\htdocs\process.php on line 11

Notice:  Undefined index: LName in C:\xampp\htdocs\process.php on line 12

Since a form submission has not occured when the page initially loads, the REQUEST array variables do not yet contain values and the indexes associated with the array are undefined. To prevent this error, we can use on of PHP's many built-in functions. In the updated example below, the the PHP isset() function used. The isset() is used to determine if a variable is set. The echo string coded within the text box is wrapped within an if... statement that checks whether the array variable has been set. Thus, the echo statement is called only if the array variable contains a value.

process.php


<!DOCTYPE html>

<html>
<head>
  <title>A Web Page</title>
</head>
<body>

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

First Name: <input type="text" name="FName" value="<?php if (isset($_REQUEST['FName'])) echo $_REQUEST['FName']?>">
Last Name: <input type="text" name="LName" value="<?php if (isset($_REQUEST['LName']))echo $_REQUEST['LName']?>">


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

</form>

</body>
</html>

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


TOP | NEXT: Chapter 8 - Sessions