Page 1 of 1

Session Not Updating until 2nd Submit

Posted: Wed Jan 25, 2006 3:06 pm
by MikeCXT
UPDATE:

my question is now why is this the case. I have provided all code and explaination below in my response, but I also found the fix, though I do not understand why this is the fix.

Code: Select all

$_SESSION = $_POST;
This was on the 2nd page only. I added it to the first page also and the problem was fixed.

Does SESSION take priority over POST when both exist? I am not sure what happens exactly, except now all POST variables are converted to SESSION varaibles before anything else happens, and that seemed to have fixed the problem. I'd like to know why that did though so I can understand it more fully.

Oh and thanks feyd, when I was posting the code as you asked, is when I noticed I only had the post to session conversion on one page, and curious if that could be the issue, tried it on both.

Posted: Wed Jan 25, 2006 3:32 pm
by feyd
psst, some code would be helpful. :)

Posted: Thu Jan 26, 2006 7:51 am
by MikeCXT
feyd wrote:psst, some code would be helpful. :)
alright alright... :) I thought maybe it was a common issue since i swear i've seen it before here, so here's the code...

Page "template.php"

Code: Select all

<form action='payment.php?".strip_tags(SID)."' method='post' name='form' onsubmit='return checkscript()'>
<table>
<tr><td><font class='require'>*</font>School Name:<br /></td><td>
<input name='schname' type='text' size='37' maxlength='50' value='$schname'><br /></td></tr>
<tr><td><font class='require'>*</font>School Phone:<br /></td><td>
<input name='schphone' type='text' size='37' maxlength='50' value='$schphone'><br /></td></tr>
</table>

<input name='submit' type='submit' value='&nbsp;&nbsp;Continue&nbsp;&nbsp;'>
So at this point, this is no SESSION data, no session, and all variables are empty.
Once you enter the data, and hit submit, you go to the 'payment.php' page, as a session. The payment page uses an if/else to see if you have valid inputs, 'if' invalid then it shows you the following:

Code: Select all

$_SESSION = $_POST;

$back_button = (!preg_match('/^\(?\d{3}\)?\s|-\d{3}-\d{4}$/',$schphone)) ||($schname == "");

if ($back_button) {
  print "<table border=0><tr><td>";
  if ( $schname == "")
	{ echo "Your school name is required.<br />"; }
  if (!preg_match('/^\(?\d{3}\)?\s|-\d{3}-\d{4}$/',$schphone))
	{ echo "Your school phone is required. (###) ###-#### or ###-###-####.<br />"; }
  echo "<FORM action='template.php?".strip_tags(SID)."' method='post' name='form'><INPUT name='submit' type='submit' VALUE='Back'></FORM><p>&nbsp;</p>";
  echo "</td></tr></table>";
}
else {
   //All inputs are shown in a small table, and additional payment inputs are now asked for with an additional submit leading to a final page that is not part of this problem 
}
So at this point, we are at the payment.php?SID page, session variables exist. All $_POST was converted to $_SESSION. A back button is printed, which is actually just a normal form submit leading back to template.php but now with a SID attached to it. When you hit submit and go back to the first page, the fields are now autopopulated due to the session data existing this time around. So, if you now correct the error, saying for School Phone you had invalid data of "893-123asj", you change it to "193-193-1933" which is valid. When you hit submit, payment.php?SID still generates the exact same error, even though the data is now valid. So you hit payment's submit, bringing you back to template (page 1). This time the field in question has the correct data autopopulated in it (so there should have been no error). Now 2 things can happen:

a)you do not change anything, leaving the valid data as is, and hit submit again, payment.php?SID this time follows the 'else' statement, showing the rest of the page since all data is valid.

b)you change the field in question to contain invalid data again, hit submit, and still payment.php?SID follows its 'else' statement for having all valid data, even though you just created invalid data.

This is what I mean by it taking 2 submits to realize what you've done.

EDIT: This is now solved, my question in the first post I editted is now what I am curious about. Thanks.