Multi page form, passing var's

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
Hibba
Forum Commoner
Posts: 58
Joined: Fri Oct 29, 2004 11:37 pm

Multi page form, passing var's

Post by Hibba »

I am having trouble making a multi page (3) form where each page is an html page (page1.html, page2.html, page3.html) and then passing the variables from the previous form to the next.

I am using a hidden form like this:

Code: Select all

<input type="hidden" name="age" value="<?php echo $_GET&#1111;'age'] ?>">
Globals are turned off (and will stay that way), but the breakdown is getting the page1 variable to page 3. I can get it to page2 via the url line.(and actually, I wish it didn't)

I think it is important to note that I am using HTML pages because of design reason, but if someone has a better idea, feel free to suggest it. Each page would look different, aside from the form being different.
Please help!

Thanks!!
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

If you are using HTML pages, I recommend using a "processing" page in between the steps of the form.

Then all of the variables you intake within each step can either be stored via a database (attached to the user through a cookie and IP), multiple $_COOKIE vars, or multiple $_SESSION vars.

I think those methods fit perfectly into the way you want the process to work.
Hibba
Forum Commoner
Posts: 58
Joined: Fri Oct 29, 2004 11:37 pm

Post by Hibba »

The only issue with that is what if the user has cookies turned off.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

The code that you showed in your first post should allow you to pass on the data. What is the actual output in html? Check to make sure that is ok.

Side Note: As a user i hate places where i have to keep filling in forms on different pages, as i have to keep loading more pages when i think i have finished filling things out. I prefer to see all the form that i need to fill out on a single page.
Hibba
Forum Commoner
Posts: 58
Joined: Fri Oct 29, 2004 11:37 pm

Post by Hibba »

Ok, I will try to make this a little easier. One reason I am stepping through different pages is design. For thte purpose of this form, it will give you advice on mortgages.

Check it the 2 html pages and maybe you can understand a little bit better:
This is page 1

Code: Select all

<html>

	<head>
		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
		<meta name="generator" content="Adobe GoLive">
		<title>PHP TEST</title>
	</head>

	<body bgcolor="#ffffff">
		<p>PHP Test form for Mortgage company</p>
		<form id="FormName" action="phptest2.html" method="GET" name="FormName">
			How old are you?  <input type="text" name="age" value="" size="2" maxlength="2">
			<p><input type="submit" name="submit" value="Next"></p>
		</form>
		<p></p>
	</body>

</html>
This is page 2

Code: Select all

<html>

	<head>
		<meta name="generator" content="Adobe GoLive">
		<title>Untitled Page</title>
	</head>

	<body bgcolor="#ffffff">
		<form id="FormName" action="phptest3.html" method="GET" name="FormName">
			<input type="hidden" name="age" value="<?php echo $_GET&#1111;'age'] ?>">
		Are you <select name="gender" size="1">
				<option>Choose</option>
				<option value="Male">Male</option>
				<option value="Female">Female</option>
			</select> ?
			<p><input type="submit" name="submit" value="Next"></p>
		</form>
		<p></p>
	</body>

</html>
This will eventually go on for 6 questions, so imagine the next page just adding another hidden value from page 2 (gender)

This html code is really only the form part of the code. In the end, it will be placed in the middle of a webpage that shows what question you are on, with text specific to help you understand the question and develop your answer.
Hibba
Forum Commoner
Posts: 58
Joined: Fri Oct 29, 2004 11:37 pm

Post by Hibba »

To help see this, here is the html layout of what it will look like:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

	<head>
		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
		<meta name="generator" content="Adobe GoLive 6">
		<title> - Get your own mortgage prescription in minutes!</title>
	</head>

	<body bgcolor="#f0e68c">
		<p><br>
		</p>
		<div align="center">
			<table width="650" border="0" cellspacing="2" cellpadding="10" align="center" bgcolor="#6b8e23">
				<tr>
					<td colspan="2" align="center" bgcolor="#6b8e23">Banner image<br>
						<br>
					</td>
				</tr>
				<tr>
					<td align="center" valign="middle" bgcolor="#6b8e23" width="150"><font size="2" color="white" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"><b>Question 1 -<br>
								How long are you going to be in your house?</b></font><font size="2" color="white" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"><b><br>
							</b><br>
							<br>
						</font><font size="2" color="#dcdcdc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">Question 2<br>
							<br>
							<br>
							Question 3<br>
							<br>
							<br>
							Question 4<br>
							<br>
							<br>
							Question 5<br>
							<br>
							<br>
							Question 6<br>
							<br>
							<br>
							Mortgage Rx</font></td>
					<td align="center" valign="middle" bgcolor="white">
						<div align="left">
							<p><font size="2" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">Welcome! In just a few short minutes, you'll have your own mortgage prescription and be on your way to financing your home!<br>
									<br>
									If you'd prefer to talk to a mortgage officer instead of using the Internet, just give , your mortgage specialist, a call at xxxxxx<br>
								</font><font size="2" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">Okay, let's get started. You'll be asked a few questions to find out the particulars of your situation. Then, at the end, you'll get a customized recommendation of what kind of loan would likely best suit your needs and goals.<br>
									<br>
									<b>Question 1:  How long do you plan to be in your home?<br>
										<br>
										Answer:  <input type="text" name="number_of_years" value="" size="2" maxlength="2" border="0"> years<br>
										<br>
									</b>If you're not sure, just give it your best guess. The reason this is important is because there are a wide variety of loans, and some make much more sense for short-term owners than for long-term owners.<br>
									<br>
									<br>
								</font></p>
						</div>
						<div align="center">
							<p><button name="nextpage1" value="I'm ready for the next question!" type="button"><font size="2" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">I'm ready for the next question!</font></button></p>
						</div>
					</td>
				</tr>
				<tr>
					<td colspan="2" align="center" valign="middle" bgcolor="#6b8e23">
						<table width="450" border="0" cellspacing="2" cellpadding="0" align="right" height="50">
							<tr>
								<td><br>
									picture here</td>
								<td bgcolor="#6b8e23">
									<div align="center">
										<font size="2" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"><b>Your Mortgage Specialist<br>
											</b></font></div>
								</td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
			<br>
			<br>
			<font size="2" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"><a href="link.htm">About Us</a> | <a href="link.htm">Programs for Investors</a> | <a href="link.htm">Refinancing</a> | <a href="link.htm">Repairing Your Credit</a></font></div>
	</body>

</html>
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

sessions will still prob be the easiest.
if cookies arent enabled, you could pass the sid through the query string(php can do all that automatically if you werent aware)

but if you dont want the sid in the query string(which there are a few good reasons not too depending on the site),
usng hidden fields like your doing is prob the best bet.
Hibba
Forum Commoner
Posts: 58
Joined: Fri Oct 29, 2004 11:37 pm

Post by Hibba »

With this form, I do not passing through the url line. So how can I do this from html to html to html. It will work now with carrying page 1 variable to page 2, but not page 1 variable to page 3.

Thanks!
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

Hibba wrote:With this form, I do not passing through the url line.
you mean your using POST, not GET ?

Code: Select all

<form method="post" action="script.php?sid=<?php echo session_id(); ?>">
<input ......
</form>
again, php can do that automatically for all links

ini_set('session.use_trans_sid', true);


but if thats no an option, my next choice would be to designate a processing page as suggested above.

something like this this could be included()

Code: Select all

$hidden_fields = '';

$fields = array( /// define ALL possible fields
    'name',
    'number',
    'foo'
);

foreach ($fields as $field) {
    if (isSet($_POST['field'])) {
        $hidden_fields .= '<input type="hidden" name="'.$field.'" value="'.$_POST[$field].'">';
    }
}



echo $hidden_fields;

?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I would be easier managing all the hidden inputs if you implemented a Page Controller that managed all three pages.
Hibba
Forum Commoner
Posts: 58
Joined: Fri Oct 29, 2004 11:37 pm

Post by Hibba »

Sorry for the bad english on that one reply...
I would not mind the variables passing through the URL line. Not a problem, but to get them from page 1 to page 3 is not working.

The processing page sounds like a possibilty. I am very new to PHP, so I do not understand this as quick as most poeple...

I am assuming that the HTML page 1 would point (action) to the processiing.php, but how would it then direct you to the html page 2? And then once on that page, it goes into the processing.php, but wouldn't the values of the first variable from page 1 be null because it has left the php code?

Thanks so much for helping me out.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

thats why we are saying to use sessions. sessions DO NOT require cookies. in the absence of cookies php can add ?sid=342524352345345234 to every relative url link in the outputted html automatically and transparently for you. it just plain works, and all there is to it is add the ini_set bit to your code. and then, you will have the form information available to you on ANY php page.



you could also ditch the html pages, and make it all into 1 php page.
then just use

<a href=form_processor.php?page=1

and let form_processor.php decide which form to show depending on what "page="
Hibba
Forum Commoner
Posts: 58
Joined: Fri Oct 29, 2004 11:37 pm

Post by Hibba »

Thanks, I will look into this with the sessions. I do know how to make it work with PHP pages and a friend of mine helped me by putting it in one single PHP file, but the way the design is for the html, it would be nice to have snippets to place on each page. I do know that the HTML can be placed within a PHP file for each of the different questions, but then to edit that HTML would not be as easy.
Post Reply