Forms provide a way to send user values and choices to the server. Forms are composed of text controls, selection lists, radio buttons, checkboxes, multi-line text and other input controls. When the user clicks submit button( or in some case presses enter), the user-supplied values and choices made are sent to the server. If there is some problem with the data supplied by the user (i.e. form submission fails), the values supplied by user and the choices made must be restored. So that he/she don't have to type it again.
The following code is a small form with different type of HTML controls, has the ability to restore the values supplied and options made. Copy and paste this code into a blank PHP file and execute to see how the values are restored.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Restoring values</title>
</head>
<body>
<form name="form1">
<table width="450px" cellpadding="3px" cellspacing="1px" bgcolor="#CCCCCC">
<tr bgcolor="#FFFFFF">
<td>Username:</td>
<td><input type="text" name="username" value="<?=$_REQUEST['username']?>" /></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>Password:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>Language:</td>
<td>
<select name="language">
<option value="english">English</option>
<option value="arabic">Arabic</option>
<option value="spanish">Spanish</option>
<option value="french">French</option>
<option value="other">Other</option>
</select>
<script language="javascript">
<?if($_REQUEST['language']!=''){?>
document.form1.language.value='<?=$_REQUEST['language']?>';
<? } ?>
</script>
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td>Gender:</td>
<td>
<input type="radio" name="gender" value="male" /> Male
<input type="radio" name="gender" value="female" /> Female
<?if($_REQUEST['gender']!=''){?>
<script language="javascript">
<?if($_REQUEST['gender']=='male'){?>
document.form1.gender[0].checked=true;
<?}else{ ?>
document.form1.gender[1].checked=true;
<? } ?>
</script>
<? } ?>
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td valign="top">Address:</td>
<td>
<textarea name="address" rows="3" cols="30"><?=$_REQUEST['address']?></textarea>
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td> </td>
<td><input type="submit" value="Save" /></td>
</tr>
</table>
</form>
</body>
</html>
There are different techniques to set values of different HTML controls. Each control is discussed individually
Text Control
<input type="text" name="username" value="<?=$_REQUEST['username']?>" />
The text control has a 'value' attribute which can be used to set default text for this control. The value attribute in above example sets the value to what was previously typed by the user in this control, if there is no previous text, nothing will be displayed
Selection Control
<?if($_REQUEST['language']!=''){?>
document.form1.language.value='<?=$_REQUEST['language']?>';
<? } ?>
The HTML selection list has no value attribute, to select a value javascript is used. Here a check is necessary to make sure that the language parameter is not blank. Also the single quotes around the request parameter as required as the language values are text, no single quotes are necessary for numeric values.
Radio Button and Checkboxes
<?if($_REQUEST['gender']!=''){?>
<script language="javascript">
<?if($_REQUEST['gender']=='male'){?>
document.form1.gender[0].checked=true;
<?}else{ ?>
document.form1.gender[1].checked=true;
<? } ?>
</script>
<? } ?>
Restoring the values of checkboxes and radio buttons is tricky. Mostly these controls come in groups, and within each group all radio buttons or checkboxes have same names (as the above example has a group i.e. gender with two radio buttons). Javascript treats every group as array and to access individual elements of array, we have to specify its index
Textarea
<textarea name="address" rows="3" cols="30"><?=$_REQUEST['address']?></textarea>
Restoring the value of textarea is easy. Everything between the opening and closing textarea tags is displayed in the textarea. So the above code will simply place the previously supplied text into the textare.
It is not recommended to restore ( or send back from server to browser) the value of a password field.