Link to home
Start Free TrialLog in
Avatar of newbe101
newbe101

asked on

php session switch problem

Hi all,
I have a multipage form and I am using php sessions to remember the user form data.  I need to give the user the option of "jumping around" the multi page form rather than forcing the user to go through the form with the submit/reset html buttons.  So, in the nav links I have included a javascript to submit the form (... onclick="document.['byc'].submit(); return false" ...).  I have also included 2 variables in the url (which page coming from "a=page number" and which page going to "z=page number").  I am using php switch on the index.php page to check for the "from page" and the "to page" then include() the appropriate contents file.  When I had this setup using submit/reset html form buttons, everything was working fine.  Now that I need to use "jumpable" links, I can't seem to get the php session to get the user form data... please help.
:::index.php:::
<?
$color=$_GET["color"];
$rims=$_GET["rims"];
$ctop=$_GET["ctop"];
$z=$_GET["z"];
 
switch ($_GET['a']) {
  case '1': 
  	session_start();
  break;
  case '2':
  	session_start();
	session_register('test1');
	session_register('test2');
	session_register('test3');
	$_SESSION['test1'] = $_POST['test1'];
	$_SESSION['test2'] = $_POST['test2'];
	$_SESSION['test3'] = $_POST['test3'];
  break;
  case '3': 
  	session_start();
  break;
  case '4': 
  	session_start();
  break;
  default: $a = ''; break;
}
 
switch ($_GET['z']) {
  case '1': $z = "1.php"; break;
  case '2': $z = "2.php"; break;
  case '3': $z = "3.php"; break;
  case '4': $z = "4.php"; break;
  default: $z = "1.php"; break;
}
 
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>BYC PHP Session Test</title>
</head>
 
<body>
<? include("nav.php"); ?>
<? include($z); ?>
</body>
</html>
:::EOF:::
 
:::nav.php:::
<form name="byc" method="post" action="<?=$z;?>">
<a href="index.php?rims=<?=$rims?>&color=<?=$color?>&ctop=<?=$ctop?>&a=1&z=1" onclick="document.['byc'].submit(); return false">Page 1</a> | 
<a href="index.php?rims=<?=$rims?>&color=<?=$color?>&ctop=<?=$ctop?>&a=1&z=2" onclick="document.['byc'].submit(); return false">Page 2</a> | 
<a href="index.php?rims=<?=$rims?>&color=<?=$color?>&ctop=<?=$ctop?>&a=1&z=3" onclick="document.['byc'].submit(); return false">Page 3</a> | 
<a href="index.php?rims=<?=$rims?>&color=<?=$color?>&ctop=<?=$ctop?>&a=1&z=4" onclick="document.['byc'].submit(); return false">Page 4</a><br />
:::EOF:::
 
:::2.php:::
<img src="gdimage.php?rims=<?=$rims?>&color=<?=$color?>&ctop=<?=$ctop?>">
<br />
test1 <input name="test1" value="<? if(isset($_SESSION['test1']))print $_SESSION['test1']; ?>" />
<br /> 
test2 <input name="test2" value="<? if(isset($_SESSION['test2']))print $_SESSION['test2']; ?>" />
<br />
test3 <input type="checkbox" name="test3" value="test3" <? if(isset($_SESSION['test3']))echo "checked"; ?> /><br />
<input type="submit" name="submit" value="Continue" /> 
</form>
<br />
<? print_r ($_SESSION); ?>
:::EOF:::
 
:::3.php:::
<img src="gdimage.php?rims=<?=$rims?>&color=<?=$color?>&ctop=<?=$ctop?>">
<br />
<?php
  print $_SESSION['test1'].'<br />';
  print $_SESSION['test2'].'<br />';
  print $_SESSION['test3'].'<br />';
?>
<br />
<? print_r ($_SESSION); ?>
<br />
<a href="index.php?rims=<?=$rims?>&color=<?=$color?>&ctop=<?=$ctop?>">Back to page 1</a>
<input type="submit" name="submit" value="Continue" /> 
</form>
:::EOF:::

Open in new window

Avatar of newbe101
newbe101

ASKER

oops... I see an issue... in nav.php I say everything is coming from page 1...
here is my revised page 2 and 3 (which still doesn't collect the user form data in the php session):
:::2.php:::
<form name="byc" method="post" action="<?=$z;?>">
<a href="index.php?rims=<?=$rims?>&color=<?=$color?>&ctop=<?=$ctop?>&a=2&z=1" onclick="document.['byc'].submit(); return false">Page 1</a> | 
<a href="index.php?rims=<?=$rims?>&color=<?=$color?>&ctop=<?=$ctop?>&a=2&z=2" onclick="document.['byc'].submit(); return false">Page 2</a> | 
<a href="index.php?rims=<?=$rims?>&color=<?=$color?>&ctop=<?=$ctop?>&a=2&z=3" onclick="document.['byc'].submit(); return false">Page 3</a> | 
<a href="index.php?rims=<?=$rims?>&color=<?=$color?>&ctop=<?=$ctop?>&a=2&z=4" onclick="document.['byc'].submit(); return false">Page 4</a><br />
<img src="gdimage.php?rims=<?=$rims?>&color=<?=$color?>&ctop=<?=$ctop?>">
<br />
test1 <input name="test1" value="<? if(isset($_SESSION['test1']))print $_SESSION['test1']; ?>" />
<br /> 
test2 <input name="test2" value="<? if(isset($_SESSION['test2']))print $_SESSION['test2']; ?>" />
<br />
test3 <input type="checkbox" name="test3" value="test3" <? if(isset($_SESSION['test3']))echo "checked"; ?> />
</form>
<br />
<? print_r ($_SESSION); ?>
:::EOF:::
 
:::3.php:::
<form name="byc" method="post" action="<?=$z;?>">
<a href="index.php?rims=<?=$rims?>&color=<?=$color?>&ctop=<?=$ctop?>&a=3&z=1" onclick="document.['byc'].submit(); return false">Page 1</a> | 
<a href="index.php?rims=<?=$rims?>&color=<?=$color?>&ctop=<?=$ctop?>&a=3&z=2" onclick="document.['byc'].submit(); return false">Page 2</a> | 
<a href="index.php?rims=<?=$rims?>&color=<?=$color?>&ctop=<?=$ctop?>&a=3&z=3" onclick="document.['byc'].submit(); return false">Page 3</a> | 
<a href="index.php?rims=<?=$rims?>&color=<?=$color?>&ctop=<?=$ctop?>&a=3&z=4" onclick="document.['byc'].submit(); return false">Page 4</a><br />
<img src="gdimage.php?rims=<?=$rims?>&color=<?=$color?>&ctop=<?=$ctop?>">
<br />
<?php
  print $_SESSION['test1'].'<br />';
  print $_SESSION['test2'].'<br />';
  print $_SESSION['test3'].'<br />';
?>
<br />
<? print_r ($_SESSION); ?>
<br />
<a href="index.php?rims=<?=$rims?>&color=<?=$color?>&ctop=<?=$ctop?>">Back to page 1</a>
</form>
:::EOF:::

Open in new window

You will avoid considerable confusion if you make session_start() the first statement in EVERY script - it should not be part of a switch or if construct.

I'm not sure I understand why your application needs to be designed this way, but I would suggest that you keep the data in a data base (not in the session) because you would presumably want to have this data available after the client closes the browser window.  

Each page could post to the same action script, and the action script would just take the contents of $_POST and use it to add information fields to the data base table.  A flag passed as a hidden variable could tell the action script what page to load after it had stored the data in the data base.

Does that make sense? ~Ray
yeah, it make sense.  On the last page, I am going to add the user selected form data to a database.  I don't need to keep the session info indefinately, so I don't want to add it unless the user adds it on the last page.  
I think I may have a logical issue here that I am not wrapping my head around quite yet.  Lets say my form fields are contained within 4 pages.  If I force the user to go from page 1 through 4 in order using submit buttons, I can make it work.  My problem is I need to give the user the ability to go to any page they want to in no particular order.  So, I tried using a switch/case to add the user form info into the session and javascript to submit the form from a link... but it is not working.
I would take they through the form step by step.  Form 1 submits to Script 2.  Script 2 updates the data base and redirects to Form 2.  Form 2 submits to Script 3.  Script 3 updates the data base and redirects to Form 3, etc.

This way you will keep all the client data, even if they have a browser hiccup - they won't have to fill in all those forms again!

You can put links on the bottom of each page to allow them to go to any form.  Each form page reads the data base and preloads its information into the form using something like this:

HTH, ~Ray
// QUERY SUCCEEDS, DATA IN $row ARRAY
$myData = $row["myData"];
 
// NOW SWITCH TO HTML
<form action=...
<input type="text" name="myData" value="<?php echo $myData; ?>" />

Open in new window

I don't want to use a database until the user finishes (which is not a part of my issue).  Also, I can't take the user through the form, I need them to have the ability to skip pages, then go back if they want.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Have you read the grading guidelines?  
https://www.experts-exchange.com/help.jsp#hi403

The central issue here that prevents your success is that the application design premise is flawed.  I did my best to get you out of trouble, but you can't expect EE to write your programs for you.  If you have a succinct question you can expect a succinct answer.  When the question is as vague as "I can't seem to get the php ...", well, I wish you good luck.

Please read the grading guidelines and reconsider the grade of "B" -- if you choose to proceed with this grade, you might want to ask yourself why the Experts would allocate a lot of effort to helping you next time.

Best regards, ~Ray
I didn't use any of your suggestions... I solved my issue another way.  Thank you for participating.
Then you might want to look at the guidelines that request you to post the solution you chose instead!  Really, this is just basic EE etiquette.  We are here to share solutions, and when you reject one path and choose another it benefits the community to present your chosen answers and the reasons.

Looking forward to seeing the code you are using to solve the issue. ~Ray
I am not even going to show you the code.  You are quite the whiney one aren't you... go complain to the moderators.  
OK, I'm not going to worry about it.  The moderators have more important things to deal with.  You have an invisible answer that is not worth a PAQ.  Good luck with your project.