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" />&nbsp;Male
				<input type="radio" name="gender" value="female" />&nbsp;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>&nbsp;</td>
			<td><input type="submit" value="Save" /></td>
		</tr>
 
	</table>
	</form>
</body>
</html>

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <b> <address> <blockquote> <br> <caption> <center> <code> <dd> <del> <div> <dl> <dt> <em> <font> <h2> <h3> <h4> <h5> <h6> <hr> <i> <img> <li> <ol> <p> <pre> <span> <strong> <sub> <sup> <table> <tbody> <td> <tfoot> <th> <thead> <tr> <u> <ul> <tr>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Use the special tag [adsense:format:group:channel] or [adsense:block:location] to display Google AdSense ads.

More information about formatting options

To combat spam, please enter the code in the image.