Jump to content

Multi page forms, losing the session variable


nharding99
Go to solution Solved by nharding99,

Recommended Posts

I'd describe myself as a "basic" php programmer....

 

I'm trying to build my first multi-page forms.

 

The first page is calculator_q1.php

No session variables are needed here, I have omitted a few options, the code looks like this

<form id="page1" method="post" action="calculator_q2.php">
<h1>1. Which best describes the primary and secondary sports combination that will be played on your pitch?</h1>
<table>
<tr><td>Rugby only</td><td><input type="radio" name="1_primary_secondary_combo" value="Rugby only"></td></tr>
<tr><td>Rugby/Soccer</td><td><input type="radio" name="1_primary_secondary_combo" value="Rugby Soccer"></td></tr>
<tr><td>Soccer/Hockey</td><td><input type="radio" name="1_primary_secondary_combo" value="Soccer Hockey"></td></tr>
</table>
<input class="blue" type="submit" value="Next">
</form>

The 2nd page is calculator_q2.php. This has some php to start the session, copy the result of form 1 to a session var, I do a debug echo and the variable is printed correctly so I think have captured it correctly. In extract, I do this:

<?php
 
	//Start the session
	session_start();		 
						 
	//Store our posted values in the session variables
	$_SESSION['1_primary_secondary_combo'] = $_POST['1_primary_secondary_combo'];					 
	
?>
<?php
	$debug=True;
	if ($debug) {
		echo ("Debug: session for q1 = ".$_SESSION['1_primary_secondary_combo']);
	}
?>
<form id="page2" method="post" action="calculator_q3.php">
<h1>2. Please choose a preferred surface</h1>
<table>
<tr><td>3G Rubber Crumb Filled Turf</td><td><input type="radio" name="2_preferred_surface" value="3G Rubber Crumb Filled Turf "></td></tr>
<tr><td>Sand Filled Turf</td><td><input type="radio" name="2_preferred_surface" value="Sand Filled Turf"></td></tr>
<tr><td>Sand Dressed Turf or<br>a Water Based Surface</td><td><input type="radio" name="2_preferred_surface" value="Sand Dressed Turf or a Water Based Surface"></td></tr>
</table>
							
<p>

<script>
function submitForm(action){
	document.getElementById('page2').action = action;
	document.getElementById('page2').submit();
}
</script>


<input class="blue" type="button" onclick="submitForm('/calculator_q1.php')" value="Previous" />
<input class="blue" type="button" onclick="submitForm('/calculator_q3.php')" value="Next" />
						
</form>

So far so good, but when I introduce page 3 (calculator_q3.php) then it prints the previous page's (from page 2) variable but can't find the session variable from question 1. This is from calculator_q3.php:

<?php
 
	//Start the session
	session_start();		 
						 
	//Store our posted values in the session variables
	$_SESSION['2_preferred_surface'] = $_POST['2_preferred_surface'];

?>
<?php
$debug=True;
if ($debug) {
	echo ("Debug: session for q1 = ".$_SESSION['1_primary_secondary_combo']);
	echo ("<br>Debug: session for q2 = ".$_SESSION['2_preferred_surface']);
}
?>

<form id="page3" method="post" action="calculator_q4.php">
<h1>3. Please choose one watering option</h1>
<table>
<tr><td>With a Rain Gun System</td><td><input type="radio" name="3_watering_option" value="With a Rain Gun System"></td></tr>
<tr><td>With a Rain gun System + Water Borehole</td><td><input type="radio" name="3_watering_option" value="With a Rain gun System + Water Borehole"></td></tr>
<tr><td>Not Required</td><td><input type="radio" name="3_watering_option" value="Not Required"></td></tr>							
</table>					
<p>
						
<script>
function submitForm(action){
	document.getElementById('page3').action = action;
	document.getElementById('page3').submit();
}
</script>

	<input class="blue" type="button" onclick="submitForm('/calculator_q2.php')" value="Previous" />
	<input class="blue" type="button" onclick="submitForm('/calculator_q4.php')" value="Next" />
</form>

To see this in action see: http://www.sports.hardingweb.net/calculator_q1.php

 

Any insights would be appreciated. Many thanks.

Link to comment
Share on other sites

Can't find a session var? 

 

1 - you have to start the session at the top of every php script.

2 - Are you doing anything "special" with your sessions such as re-establishing the session id to avoid hackers and such?  I doubt that at your level you would be doing it, but I have to ask.

3 - Are you doing any "unset" function on your session vars?  Or on $_SESSION itself?

 

PS - you do use uppercase for the name '$_SESSON' all the time, yes?

Link to comment
Share on other sites

your site isn't setting any session id cookie. it's likely that your session_start() statement isn't positioned in the code on your page before you output anything else to the browser.

 

setting php's error_reporting to E_ALL and display_errors to ON or log_errors to ON would help you by displaying or logging all the php detected errors.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.