1,105,556 Community Members

Retaining textbox values

Member Avatar
heels
Light Poster
32 posts since May 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi,

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

Regards,
heels

Member Avatar
R0bb0b
Posting Shark
995 posts since Jun 2008
Reputation Points: 344 [?]
Q&As Helped to Solve: 92 [?]
Skill Endorsements: 3 [?]
 
0
 

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"

Member Avatar
heels
Light Poster
32 posts since May 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
nav33n
Purple hazed!
3,894 posts since Nov 2007
Reputation Points: 472 [?]
Q&As Helped to Solve: 381 [?]
Skill Endorsements: 20 [?]
Team Colleague
Featured
 
0
 

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 />
Member Avatar
heels
Light Poster
32 posts since May 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
casper_wang
Junior Poster in Training
67 posts since Jul 2008
Reputation Points: 1 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

try looking at this great example:
http://www.guidesandtutorials.com/dreamweaver_8_tutorial_insert_toolbar.html

Member Avatar
heels
Light Poster
32 posts since May 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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. :)

Member Avatar
casper_wang
Junior Poster in Training
67 posts since Jul 2008
Reputation Points: 1 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
0
 
Member Avatar
heels
Light Poster
32 posts since May 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

try looking at this great example:
http://www.guidesandtutorials.com/dr...t_toolbar.html

Hi,

There is no such URL at the domain. Mind pasting me the whole URL again? Thanks.

Regards,
heels

Member Avatar
casper_wang
Junior Poster in Training
67 posts since Jul 2008
Reputation Points: 1 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
0
 

sorry that link was for another post.
you need to look here:
http://www.evolt.org/article/Clever_forms_with_PHP/17/60143/index.html

Member Avatar
Venom Rush
Posting Whiz
363 posts since Oct 2007
Reputation Points: 21 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
casper_wang
Junior Poster in Training
67 posts since Jul 2008
Reputation Points: 1 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
1
 

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.

Question Answered as of 5 Years Ago by casper_wang, Venom Rush, nav33n and 1 other
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article