Page 1 of 1

need help on form processing

Posted: Mon Mar 15, 2004 9:02 am
by gzmabe
I design a form in html, and use a php file to handle the validation check, the check just basically go arround all the variable from the form, if the user didnt complete the form, just output a message and go back to the html, but all the value the user input before will gone, i want to know how can i keep the value on the html form when i go back againi
I mean how can i save down the value when the user first input and paste back the the form automactically when i link back to the form

Posted: Mon Mar 15, 2004 9:51 am
by Goowe
I'm not a great coder, but I was once shown this idea from someone who was. A lot of the time, browser's will remember the fields, too... I dunno, I hope this helps you out.

Code: Select all

<?php
if(!isset($_POST['form']))
{
	// Display Form -- The hidden 'form' value hasn't been hit yet
	echo "<form method=post>";
	echo "First Name: <input type=text size=30 name='first_name' value='".$_SESSION['first_name']."'><BR>";
	echo "Last Name: <input type=text size=30 name='last_name' value='".$_SESSION['last_name']."'><BR>";
	echo "<input type=submit value='Submit'>";
	echo "<input type=hidden name='form'>";
	echo "</form>";
}
	else
{
	// Checking Form

   # Set a session with the value for each input field that the person last put in...
   foreach($_POST as $k => $v)
   {
		session_register($k);
		$_SESSION[$k] = $v;
   }

	if(!empty($_POST['first_name']) && !empty($_POST['last_name']))
	{
		# The user has filled in the required forms... do what you want with the info and clear the sessions
		echo "First Name: ".$_POST['first_name']."<BR>";
		echo "Last Name: ".$_POST['last_name']."<BR>";
      session_unset();
      session_destroy();
	}
		else
	{
		echo "One or more of the required fields is not complete. Please go back and complete the form.";
	}
}
?>

Posted: Mon Mar 15, 2004 10:47 am
by nutstretch
can you not use the history.back function in html

Posted: Mon Mar 15, 2004 7:11 pm
by Goowe
To go back to the last page... yeah :roll:

I thought he was asking to keep the values for the input fields so they would be there until changed again or until the form was submitted correctly?

Posted: Mon Mar 15, 2004 7:43 pm
by tim

Code: Select all

<a href='javascript:history.go(-1)'>click me to go back</a>
this is pretty handy and should still keep your values.