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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Problem

Status
Not open for further replies.

gokeeffe

Programmer
Jan 11, 2005
170
IE
I hope someone can follow the problem I am having.

I have a three-step registration form and a confirmation page after step3

If someone wants to change a step they navigate back to the step where the field is that they

want to change, that’s no problem as I am using session variables and the items that they

selected previously appear so they know what they had selected.

The problem is this is if:

someone goes to for example step 2 for the first time, there are three fields to fill in which are

all mandatory fields, lets say they fill in the first two fields and press the button to lead them to

step 3 they have forgotten to fill in field 3 so the validation rule kick in, telling them that they

must fill in field 3, now here’s the problem the other fields that they have filled in now

become blank hence the user must fill in the other two fields again. I am using $_SESSIONS

to echo the variable in the form boxes, If I use $_POST values instead of $_SESSIONS this

problem doesn’t happen but if someone navigates back to change their values they are not

retained in the form fields as you are not echoing out session values.

I hope someone can understand what I am getting at.

Many Thanks

Graham
 
The workaround I use in situations like multi-page form inputs is to have each form submit its input back to the script that generated the form. That script can then validate the input. If the input validates, the script stores it in a session variable and uses a "Location:" header to send the browser to the next step. If the input does not validate, the script can then re-output the form with error messages and inputs pre-populated with the just-submitted values.

Let's say that we have a 3-stage input. The first page will take in the user's name and date of birth, the second will accept his address, and the third will accept his phone number. The three scripts will be called input1.php, input2.php and input3.php.

input1.php is going to look something like:

Code:
<?php
session_start();

if (isset($_POST['firstname'])
{
	/*code for validating input goes here*/
	
	if (/*the input could be validated*/)
	{
		$_SESSION['firstname'] = $_POST['firstname'];
		$_SESSION['middlename'] = $_POST['middlename'];
		$_SESSION['surname'] = $_POST['surname'];

		header ('Location: input2.php');
	}
	else
	{
		$error_string = /* something appropriate */;
	}
}
else
{
	print '<html><body><form method="POST" action="input1.php">';
	
	if (isset ($error_string))
	{
		print 'There was a probem with your input.  ' . $error_string;
	}

	print 'First name: <input type="text" name="firstname" ';
	
	if (isset ($_POST['firstname']))
	{
		print 'value="' . $_POST['firstname'] . '"';
	}
	
	print '><br>';

	print 'Middle name: <input type="text" name="middlename" ';
	
	if (isset ($_POST['firstname']))
	{
		print 'value="' . $_POST['middlename'] . '"';
	}
	
	print '><br>';

	print 'Surname: <input type="text" name="surname" ';
	
	if (isset ($_POST['surname']))
	{
		print 'value="' . $_POST['surname'] . '"';
	}
	
	print '><br>';
	
	print '<input type="submit">';
	
	print '</form></body></html>';
}
?>

input2.php and input3.php will behave similarly to input1.php, with appropriate changes to behavior to output and handle input from their respective form fields.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top