Save form typing if submission is not successful
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>
Post new comment