Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retaining values in text box

Status
Not open for further replies.

ZOR

Technical User
Jan 30, 2002
2,963
GB
What is the prefered way to keep a value inside a text box once it has been entered. In other words you can fill a box on a form, goto another page and later return to it.

Form index.htm
<?
session_start
?>

<html><body>
<form action="process.php" method="post">
<input name="quantity" type="text" />

<input type="submit" />
</form>
</body></html>



Form Process.php
<?
session_start
?>

<html><body>
<?php
$quantity = $_POST['quantity'];
echo "Entered ". $quantity . ".<br />";
?>

</body>
<a href="index.htm">back</a><p>
</html>Form index.htm
<?
session_start
?>

<html><body>
<form action="process.php" method="post">
<input name="quantity" type="text" />

<input type="submit" />
</form>
</body></html>



Form Process.php
<?
session_start
?>

<html><body>
<?php
$quantity = $_POST['quantity'];
echo "Entered ". $quantity . ".<br />";
?>

</body>
<a href="index.htm">back</a><p>
</html>

Many thanks
 
Typically, I would store the submitted values in a session-variable and reinstantiate the value using a 'value="..."' attribute when the form is re-fetched.

Something like:

Code:
<?php
session_start();

if (isset($_POST['formfield']))
{
	if (!isset($_SESSION['field_values']))
	{
		$_SESSION['field_values'] = array();
	}
	
	$_SESSION['field_values']['formfield'] = $_POST['formfield'];
	
	header('Location:  some_other_page.php');
}
else
{
	print '<html><body>
	<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">
		<input type="text" name="formfield"';
		
	if (isset($_SESSION['field_values']))
	{
		if (isset ($_SESSION['field_values']['formfield']))
		{
			print ' value="' . $_SESSION['field_values']['formfield']	 . '"';
		}
	}
	
	print '><input type="submit">
	</form></body></html>';
}
?>



Want the best answers? Ask the best questions! TANSTAAFL!
 
Many thanks. I pasted the code into a new index.php page, put in a location address that had <a href="index.htm">back</a><p> to allow going back to the index page, however the textbox was empty. In my redirect back page I had the session_start lines in. Am I understanding your information correctly. Thanks again.
 
Thanks, I tried both refreshing and not refreshing the browaer, the textbox still returns empty. Maybe I should put the value entered the first time into a temporary database file and then when the page is recalled its all reloaded into another page for view/editing. Seems a long way round but I have several pages of products with text boxes to handle. Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top