Hi,

How do you retain values entered in the textbox after submitting? javascripts or any other methods? Thanks alot.

Regards,
heels

Here are some examples for textboxes, checkboxes, radio buttons and select drop downs:

<?
session_start();

//collect text box values
$txtFname = isset($_POST['txtFname'])?$_POST['txtFname']:"";
$txtLname = isset($_POST['txtLname'])?$_POST['txtLname']:"";
$txtMI = isset($_POST['txtMI'])?$_POST['txtMI']:"";

//values for ages select element
$ages = array();
		$ages[] = "1 - 10";
		$ages[] = "11 - 20";
		$ages[] = "21 - 30";
		$ages[] = "31 - 40";
		$ages[] = "41 - 50";
		$ages[] = "51 - 60";
		$ages[] = "61 - 70";
		$ages[] = "71 - 80";
		$ages[] = "81 - 90";
		$ages[] = "91 - 100";
//selected value for ages
$selAge = isset($_POST['selAge'])?$_POST['selAge']:"";

//build and set selected values for chk boxes interests
$interests = array();
$interests["IT"] = (isset($_POST['chkInterests']) && in_array("IT", $_POST['chkInterests']))?' checked="checked"':"";
$interests["Snowboarding"] = (isset($_POST['chkInterests']) && in_array("Snowboarding", $_POST['chkInterests']))?' checked="checked"':"";
$interests["Hockey"] = (isset($_POST['chkInterests']) && in_array("Hockey", $_POST['chkInterests']))?' checked="checked"':"";
$interests["Football"] = (isset($_POST['chkInterests']) && in_array("Football", $_POST['chkInterests']))?' checked="checked"':"";

//build and set selected values for radio buttons Gender
$rdoGender = array();
$rdoGender["Male"] = (isset($_POST['rdoGender']) && $_POST['rdoGender'] == "Male")?' checked="checked"':"";
$rdoGender["Female"] = (isset($_POST['rdoGender']) && $_POST['rdoGender'] == "Female")?' checked="checked"':"";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body onload="startTime();">
<form method="post" action="<? echo $_SERVER['REQUEST_URI']; ?>">
	First Name: <input type="text" name="txtFname" value="<? echo $txtFname; ?>" /><br />
	Last Name: <input type="text" name="txtLname" value="<? echo $txtLname; ?>" /><br />
	MI: <input type="text" name="txtMI" value="<? echo $txtMI; ?>" /><br /><br />
	Age:<br />
	<select name="selAge">
		<?
			foreach($ages as $age)
			{
			?>
				<option <? if($age == $selAge) echo ' selected="selected"'; ?> value="<? echo $age; ?>"><? echo $age; ?></option>
			<?
			}
		?>
	</select><br /><br />Interests:<br />
	
	<?
		foreach($interests as $key=>$value)
		{
		?>
			<input type="checkbox" name="chkInterests[]"<? echo $value; ?> value="<? echo $key; ?>" />&nbsp;<? echo $key; ?><br />
		<?
		}
	?><br /><br />Gender:<br />
	<?
		foreach($rdoGender as $key=>$value)
		{
		?>
			<input type="radio"<? echo $value; ?> name="rdoGender" id="rdoGender" value="<? echo $key; ?>" />&nbsp;<? echo $key; ?><br />
		<?
		}
	?>
	<input type="submit" name="btnSubmit" value="Submit Form" />
</form>
</body>
</html>

If you have a form handler on a different file, you can do this on that file before redirecting back

session_start();
$_SESSION['form'] = $_POST;

Then in the top code block replace "$_POST" with "$_SESSION"

Here are some examples for textboxes, checkboxes, radio buttons and select drop downs:

<?
session_start();

//collect text box values
$txtFname = isset($_POST['txtFname'])?$_POST['txtFname']:"";
$txtLname = isset($_POST['txtLname'])?$_POST['txtLname']:"";
$txtMI = isset($_POST['txtMI'])?$_POST['txtMI']:"";

//values for ages select element
$ages = array();
		$ages[] = "1 - 10";
		$ages[] = "11 - 20";
		$ages[] = "21 - 30";
		$ages[] = "31 - 40";
		$ages[] = "41 - 50";
		$ages[] = "51 - 60";
		$ages[] = "61 - 70";
		$ages[] = "71 - 80";
		$ages[] = "81 - 90";
		$ages[] = "91 - 100";
//selected value for ages
$selAge = isset($_POST['selAge'])?$_POST['selAge']:"";

//build and set selected values for chk boxes interests
$interests = array();
$interests["IT"] = (isset($_POST['chkInterests']) && in_array("IT", $_POST['chkInterests']))?' checked="checked"':"";
$interests["Snowboarding"] = (isset($_POST['chkInterests']) && in_array("Snowboarding", $_POST['chkInterests']))?' checked="checked"':"";
$interests["Hockey"] = (isset($_POST['chkInterests']) && in_array("Hockey", $_POST['chkInterests']))?' checked="checked"':"";
$interests["Football"] = (isset($_POST['chkInterests']) && in_array("Football", $_POST['chkInterests']))?' checked="checked"':"";

//build and set selected values for radio buttons Gender
$rdoGender = array();
$rdoGender["Male"] = (isset($_POST['rdoGender']) && $_POST['rdoGender'] == "Male")?' checked="checked"':"";
$rdoGender["Female"] = (isset($_POST['rdoGender']) && $_POST['rdoGender'] == "Female")?' checked="checked"':"";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body onload="startTime();">
<form method="post" action="<? echo $_SERVER['REQUEST_URI']; ?>">
	First Name: <input type="text" name="txtFname" value="<? echo $txtFname; ?>" /><br />
	Last Name: <input type="text" name="txtLname" value="<? echo $txtLname; ?>" /><br />
	MI: <input type="text" name="txtMI" value="<? echo $txtMI; ?>" /><br /><br />
	Age:<br />
	<select name="selAge">
		<?
			foreach($ages as $age)
			{
			?>
				<option <? if($age == $selAge) echo ' selected="selected"'; ?> value="<? echo $age; ?>"><? echo $age; ?></option>
			<?
			}
		?>
	</select><br /><br />Interests:<br />
	
	<?
		foreach($interests as $key=>$value)
		{
		?>
			<input type="checkbox" name="chkInterests[]"<? echo $value; ?> value="<? echo $key; ?>" />&nbsp;<? echo $key; ?><br />
		<?
		}
	?><br /><br />Gender:<br />
	<?
		foreach($rdoGender as $key=>$value)
		{
		?>
			<input type="radio"<? echo $value; ?> name="rdoGender" id="rdoGender" value="<? echo $key; ?>" />&nbsp;<? echo $key; ?><br />
		<?
		}
	?>
	<input type="submit" name="btnSubmit" value="Submit Form" />
</form>
</body>
</html>

If you have a form handler on a different file, you can do this on that file before redirecting back

session_start();
$_SESSION['form'] = $_POST;

Then in the top code block replace "$_POST" with "$_SESSION"

Sorry for the late reply, I did try your method. But in the textbox, I'm already printing out the values from another page. For example,

<td><input name="firstName" type="text" id="firstName" value="<? echo $contact ?>" size="50" /></td>

so how do i echo the values out again?

Simple..

$txtFname = isset($_POST['txtFname'])?$_POST['txtFname']:$contact['firstName'];
//if txtFname is set, then assign that value to $txtFname, else, assign $contact['firstName'] to $txtFname.

and then,

First Name: <input type="text" name="txtFname" value="<? echo $txtFname; ?>" /><br />

Simple..

$txtFname = isset($_POST['txtFname'])?$_POST['txtFname']:$contact['firstName'];
//if txtFname is set, then assign that value to $txtFname, else, assign $contact['firstName'] to $txtFname.

and then,

First Name: <input type="text" name="txtFname" value="<? echo $txtFname; ?>" /><br />

Thanks, it is working perfectly. But I am a little confuse over here, can you help me with it?

Over here, will be an edit function. This is redirected from the main page through their ID, so it will retrieve the contact details and "echo" them into the necessary text fields. So after the person makes the necessary changes, he/she will click the "Save" button.
When the "Save" button is clicked, it will start validating that the necessary fields are filled up and with no errors, if it fits the condition, it will automatically save the changes in the database.

But if there are missing textfields or errors, it will repopulate the form again with the changes being made previously, together with the error message.

The main problem now is, if there are any errors, it will repopulate the form again with the 1st set of values being selected from the database again and not the values with the changes being made. So how do i get started?

if there are errors it should not insert into the DB so it will repopulate using what is in the DB.
are you asking how to repopulate the form with their selections only highlighting the section that caused the error?

yeah, something like that. :)

Are you trying to carry the textbox data over to a different page?

no, he wants the page to reload with all the form fields filled in with the info that was entered only highlighting incorrect or missing fields.

when you fill out an application you fill in several form fields if one field is missing or incorect it will reload with an error.
He wants the reload to re-populate the fields the user had entered that was correct.

follows the line of sessions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.