Link to home
Start Free TrialLog in
Avatar of Refael
RefaelFlag for United States of America

asked on

saving form fields state


Hello Experts,

I have a 'form to email' script in php.
when i fill in the long form and press submit then i press the back button in my browser the fields are all empty. is some online forms i see that the fields input are being saved... how is it done?
I am not sure this is really good idea (security reasons) but i would like to know how its done.

- the form is in POST mode
i tried to add a value like so: <?php echo $FirstName ?>
but it does not seem to work.

Avatar of re-searcher
re-searcher
Flag of United States of America image

you use wrong code.
use following code:
echo $_POST['first_name']

Open in new window


but if you using browser's back button it may be don't work.
Avatar of Refael

ASKER


Hi re-searcher

i tried it but it does not work.
are you sure you understand my question? i am not trying to send a variable from one page to another.
i just hit the browser back button and all the fields are empty! i am trying to keep the inputs (save state).
Oh sorry,
do you using no-cache in header or HTML attributes?
There are two moving parts in play here.  One is the form script and the other is the action script.  Your form (HTML) submits information to your action script (PHP).  In order to achieve what you want, you will need to create two-way intelligent communication between the form and action script.  And you may find that the browser "back" button is not that great an idea -- you may want a direct link from the action script back to the form script.

One way you can create the intelligent communication would be to use a data base or the PHP session.  You can experiment with this script to see how it works.
http://www.laprbass.com/RAY_temp_refael.php

HTH, ~Ray
<?php // RAY_temp_refael.php
error_reporting(E_ALL);

// ALWAYS START THE SESSION ON EVERY PAGE UNCONDITIONALLY
session_start();

// SET A DEFAULT VALUE
$cheese = NULL;

// IF THE FORM HAS BEEN POSTED RUN THE ACTION SCRIPT
if (!empty($_POST["cheese"]))
{
    // STORE THE POST DATA IN THE SESSION
    $_SESSION["cheese"] = $_POST["cheese"];

    // AND ANY OTHER PROCESSING YOU NEED HERE
    var_dump($_SESSION["cheese"]);

}   // END OF THE ACTION SCRIPT

// SET UP THE FORM SCRIPT USING HEREDOC NOTATION
if (isset($_SESSION["cheese"])) $cheese = $_SESSION["cheese"];

$form = <<<ENDFORM
<form method="post">
WHAT KIND OF CHEESE?
<input name="cheese" value="$cheese" />
<input type="submit" />
</form>
<a href="{$_SERVER["PHP_SELF"]}">Click here</a> to reload the page.
ENDFORM;

echo $form;

Open in new window

Avatar of Refael

ASKER


Hello Ray_Paseur, thank you so much for that!

you are right but i am not sure you understood the issue.
lets say i have 2 pages:

1). the form page - in php
2). the script page - in php - a script that deals with the form after the user click "submit".

i have some (server-side) validation checking for example if the email is not empty and it is valid.
in case it is empty and or is not valid i echo something like this:

"email is missing. please click here to go back  and fill in your email address".

when the user click "go back" he returns to the "PAGE 1" -> the form page.
all the fields are empty and he have to fill them all again..... that is the exact issue.

i hope this is clear now. thank you once more.
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