Page 1 of 3
Saving form contents so that they reappear
Posted: Wed Mar 24, 2004 7:10 am
by mjseaden
Dear All
I'm trying to use sessions to save the contents of a data form, so that when the user goes on to the next page (and the contents are posted), when they press 'back', they reappear so that they can still be edited.
I've tried to do this by setting the post variables derived from the data form equal to its own session variable. Then, when the user presses 'back' to get back to the data form, I have the data form check to see if these session variables are != "", and if so populate the data objects with their previous values.
However, this doesn't seem to work, and the session variables don't seem to be preserved if I press 'back'. Is this normal, and if so, is there a better way of getting this done?
Many thanks
Mark
Posted: Wed Mar 24, 2004 7:17 am
by patrikG
use $_SESSION, see php manual and session tutorial in the Tutorial forum.
Posted: Wed Mar 24, 2004 7:18 am
by magicrobotmonkey
It should work, but what you may want to do is
if(isset($_SESSION[$variable]){
echo $_SESSION[variable];
}
Posted: Wed Mar 24, 2004 7:41 am
by mjseaden
Here's the code that doesn't seem to be working:
Code: Select all
<TD HEIGHT="20" WIDTH="604" STYLE="padding-right:16px" BGCOLOR="#000066"><FONT FACE="Tahoma" SIZE="2" COLOR="#FFFFFF">
<?php
if(isset($_SESSION['item_ID'])) echo '<INPUT TYPE="text" LENGTH="100" SIZE="26" NAME="txt_itemID" VALUE="'.$_SESSION['item_ID'].'">';
else echo '<INPUT TYPE="text" LENGTH="100" SIZE="26" NAME="txt_itemID">';
echo ' e.g. DE-10-157';
?>
</FONT></TD>
And once the variables have been posted to the next page (and I have checked that they are being successfully posted), I simply place
Code: Select all
$_SESSION['item_ID'] = $_POST['txt_ItemID'];
I have session_start() at the beginning of the code for each page.
I can't see anything wrong with this, but it doesn't seem to work - the text boxes (etc.) still remain empty on pressing 'back'.
Thanks
Mark
Posted: Wed Mar 24, 2004 7:45 am
by magicrobotmonkey
when you go back does it ask to refresh?
Posted: Wed Mar 24, 2004 7:47 am
by mjseaden
No, it doesn't, which is an interesting point. No variables are posted from a previous page to the original data form - I don't know if that's an explaination?
Posted: Wed Mar 24, 2004 7:51 am
by magicrobotmonkey
oh yea true..
well do if(isset($_SESSION[$var]) echo$var; and see if it is set and blank or just not set at all. And you know they are set on the next page, right?
Posted: Wed Mar 24, 2004 7:54 am
by patrikG
Make sure that you only set $_SESSION[..]=$_POST[...] is only set if $_POST has a value. Otherwise you'll assign $_SESSION[...] an empty $_POST[...];
Posted: Wed Mar 24, 2004 8:13 am
by mjseaden
Okay - I've check by echoing on the page following the data form that both $_POST contains content, and following setting, the $_SESSION variables contain content.
Yet, when I press 'back', echoing those same session variables, that I have confirmed contain the $_POST information, indicates that they are empty, i.e. they have lost that information.
Is this to do with the way browsers work, rather than the code? I don't understand.
Posted: Wed Mar 24, 2004 8:16 am
by magicrobotmonkey
I like to make a fxn that only runs when leaving the form page which fills the $_SESSION vars in from the $_POST vars so if you have navigation you dont get all kinds of errors
Posted: Wed Mar 24, 2004 8:18 am
by mjseaden
magic - can you explain what you mean by fxn?
Posted: Wed Mar 24, 2004 8:22 am
by magicrobotmonkey
function - it doesnt sound like you are doing navigation, however, so just ignore all that
Posted: Wed Mar 24, 2004 8:24 am
by mjseaden
Any idea what is going on? Why are my $_SESSION variables being reset when I press 'back'? Is this normal?
Posted: Wed Mar 24, 2004 8:27 am
by redmonkey
Personally I think it is bad practice to rely on the browsers back button as each browser tends to have a slightly different implementaion.
I think the problem is that when you hit the back button you are pulling the page from the browsers cache not a freshly generated page.
You could add headers to the page to prevent caching which should fix the problem, although some browsers ignore these headers.
You could also create your own back button on the page that follows the form which when used in conjunction with the above may help reloading the page.
Posted: Wed Mar 24, 2004 8:32 am
by magicrobotmonkey
Oh yea good point, red, and I'm with you on creating your own button - develope a simple navigation scheme and you can use it anywhere!