need help on form processing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gzmabe
Forum Newbie
Posts: 1
Joined: Mon Mar 15, 2004 9:02 am
Location: London

need help on form processing

Post 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
Goowe
Forum Commoner
Posts: 94
Joined: Mon Mar 15, 2004 9:51 am
Location: Southeast Alaska

Post 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.";
	}
}
?>
nutstretch
Forum Contributor
Posts: 104
Joined: Sun Jan 11, 2004 11:46 am
Location: Leicester

Post by nutstretch »

can you not use the history.back function in html
Goowe
Forum Commoner
Posts: 94
Joined: Mon Mar 15, 2004 9:51 am
Location: Southeast Alaska

Post 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?
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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.
Post Reply