Page 1 of 1

Problem with loss of Form data after pressing Back button

Posted: Tue Nov 17, 2009 8:15 am
by Bob Kellock
After form data has been passed to the next page I want the user to have the option to return to the form page to edit it. In Opera, Chrome, Safari and Firefox (pre about V3.5) when you return to the form the prior data that you entered is still in the form boxes but in Firefox (V3.5.5) and IE (I've only tested in V8.0..) it has vanished.

I've managed to fudge it for text boxes but not radio buttons.

I'd like to know if there is a better way of doing it or if my fudge can be extended to work on radio buttons.

Bob

Opening page (play0.php)

Code: Select all

<?php
  session_start();
  session_register("SVname");
  session_register("SVbut");
  $_SESSION['$SVname']   = '';
  $_SESSION['$SVbut']    = '';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<!-- This page simply registers & intialises the session variables
     Initialisation of variable values would be redundant if session_unset() and session_destroy() were called   when exiting the site -->
  <head>
     <meta http-equiv="content-type" content="text/html">
     <meta HTTP-EQUIV="REFRESH" content="0; url=play1.php">
  </head>
  <body>
  </body>
</html>
play1.php

Code: Select all

<?php  session_start(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      
<html>
  <head>
    <META http-equiv=content-type content="text/html; charset=ISO-8859-1"> 
  </head>
  <body>
<?php
   $name = $_SESSION['$SVname'];
   $butt = $_SESSION['$SVbut'];
?>
  <form  enctype="multipart/form-data" action="play2.php" id="form1" method="post" name="form1">
<!-- If echo $name on the next line is replaced by echo $_SESSION['$SVname'] then $_SESSION['$SVname'] loses 
     its value on going back to the previous page -->  
       Name <input type="text" name="yourname"  size="50" value="<?php echo $name ?> onkeypress="return noenter()">  <br>
       Button1 <input type="radio" name="radiobutton"  value="1" >  <br>
       Button2 <input type="radio" name="radiobutton"  value="2" >  <br> 
       <input type="submit"  name="submit" value="Continue to next stage" >
  </form>        
</body>
</html>
 
 
 
 
 
 
 
 
 
play1.php

Code: Select all

<?php   session_start(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>
<?php
   $_SESSION['$SVname']  = $_REQUEST["yourname"];
   $_SESSION['$SVbut']   = $_REQUEST["radiobutton"]; 
 
   echo "Name   = " . $_SESSION['$SVname'] . "<br />"; 
   echo "Button = " . $_SESSION['$SVbut'] . "<br />"; 
?>
  <INPUT TYPE="button" VALUE="EDIT DETAILS " onClick="history.go(-1);" > <!-- same as pressing Back button -->
</body>
</html>
 

Re: Problem with loss of Form data after pressing Back button

Posted: Tue Nov 17, 2009 8:42 am
by iankent
I think you've gone about it the right way so far (or at least, that's how I'd do it too!). You just need to store the values of the radio buttons as well as the text boxes, then when drawing the form check for those values and output checked="checked" inside the relevant <input> tag for whichever radio is selected

hth