A better, NON-CLIENT-SIDE way to do it, is you could have the page you're
submitting to write the POST values to a session.

So the top lines of your form handler page would be something like:

<?
session_start();
foreach($HTTP_POST_VARS as $key => $val)
{
        if($val != '')
        {
                $HTTP_SESSION_VARS[$key] = $val;
        }
}
?>

And on your form page, populate the VALUE fields of the form with
conditional statements evaluating the session variables.  Like so:

<INPUT TYPE=TEXT NAME="whatever" VALUE="
<?
if(isset($HTTP_SESSION_VARS['whatever']) && $HTTP_SESSION_VARS['whatever']
!= '')
{ 
        echo $HTTP_SESSION_VARS['whatever']; 
} 
?>
" SIZE=20>

...or a quick n dirty way (suppressing errors):
<INPUT TYPE=TEXT NAME="whatever" VALUE="<? echo @$HTTP_SESSION_VARS['whatever']; ?>" 
SIZE=20>


For checkboxes, it's just a minor change:
<INPUT TYPE=CHECKBOX NAME="whatever1" 
<? 
if(isset($HTTP_SESSION_VARS['whatever1']) && $HTTP_SESSION_VARS['whatever'] != '') 
{ 
        echo 'CHECKED';
}
?>>


One caveat to consider:

If the user returns to the form and changes checkbox values to the unchecked state, 
when the form is posted the variable will not be reposted with an empty value.  Form 
checkboxes only post if they are checked, and then they evaluate to 'on' - if it is 
unchecked, there will be no $HTTP_POST_VARS value for that field.  So in the above 
session writing code, before you set the session variables with the POSTed data, you 
should loop through all of the existing session vars, setting them to ''.  Take care 
to exclude the variables that you are using to keep the user authenticated, or to 
store the other data that you will need site wide.  Like this:

<?
session_start();
foreach($HTTP_SESSION_VARS as $key => $val)
{
        // just an example of some session vars you might include
        $persistent_sitewide_session_vars = array ('loggedinstatus', 'userid', 
'browser');
        
        // well use a counter to make sure we're keeping track correctly
        $counter=sizeof($persistent_sitewide_session_vars);
        
        // Loop through each of your persistent 
        // site-wide session vars
        foreach($persistent_sitewide_session_vars as $key_p => $val_p)
        {
                // if the current session var is one of 
                // the session vars you need site wide, 
                // set it to itself and exit loop
                if($HTTP_SESSION_VARS[$key] == 
$persistent_sitewide_session_vars[$key_p])
                {
                        $HTTP_SESSION_VARS[$key] = $val;
                        break;
                }
                $counter--;
        }
        
        // if you know you looped through all the variables
        // and none of your persistent variables were found, 
        // it's safe to reset the session var to a blank var
        if($counter == 0)
        {
                $HTTP_SESSION_VARS[$key] = '';
        }
}


// NOW YOU CAN ADD YOUR CODE TO SET SESSION VARS 
// WITH THE POSTED FORM VARIABLES

?>


It's a bit bulky, but it does work like a charm.  Remember to add start_session() to 
the top of the form page if it's not there already!

Happy form hacking!

Dave



-----Original Message-----
From: Brent Baisley [mailto:[EMAIL PROTECTED]]
Sent: Friday, August 30, 2002 6:12 AM
To: Jean-Christian Imbeault
Cc: [EMAIL PROTECTED]
Subject: Re: [PHP] how to have a form keep values when user hits back
button?


As others have probably told you, you can't rely on the back button and 
don't try to disable it, it's part of the user interface. The user 
really needs to live with the fact that if they leave the page, they 
will lose the data entered.

However, there is a way to retain the data as they enter it. You can use 
Javascript to write the data to cookies as they enter it. This means 
setting traps on every field, checkboxes and radio button would have an 
onClick, data entry fields would have an onBlur (is that write?). So as 
they enter the data, it is save in local cookies.
You would also want to add an onLoad trap on the page to check for and 
read the cookies if present to populate the fields. Most people aren't 
used to cookies management via Javascript, so it may be a little 
daunting at first. But this will work. If cookies javascript
 are enabled
that is.


On Thursday, August 29, 2002, at 05:22 AM, Jean-Christian Imbeault wrote:

> I have php page that creates an html form. When the user hits the
> submit button another php script is called to parse the form and
> display some output.
>
> However I find that if I hit the back button the form values get reset.
> How can I make it so that if I hit the back button the values I entered
> in the form will still be displayed?
>
> Jc
>
> -- PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
Brent Baisley
Systems Architect
Landover Associates, Inc.
Search & Advisory Services for Advanced Technology Environments
p: 212.759.6400/800.759.0577


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to