[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

saving form fields state

Posted on 2011-10-17
6
Medium Priority
?
173 Views
Last Modified: 2012-05-12

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.

0
Comment
Question by:Refael
  • 2
  • 2
  • 2
6 Comments
 
LVL 3

Expert Comment

by:re-searcher
ID: 36982947
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.
0
 

Author Comment

by:Refael
ID: 36982993

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).
0
 
LVL 3

Expert Comment

by:re-searcher
ID: 36983016
Oh sorry,
do you using no-cache in header or HTML attributes?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36993566
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

0
 

Author Comment

by:Refael
ID: 36994501

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.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 36994906
That will not be an issue if you follow the design pattern I showed you.  See line 21 and line 27 of the script above.  It may be easier than you think!

A normal and useful way of designing a form+action script is to keep the two scripts in one script file.  What you describe as two "pages" are really two HTTP requests to the server.   Both requests go to the same script.  One of these, a GET method request has the $_POST["cheese"] set to empty.  So the script generates the form.  The other request is a POST request that is made upon submit of the form.  If there is anything in the $_POST["cheese"] variable, it will be copied into the session.  The next request, no matter what it is, will have the session array with the value of cheese (if any) in the $_SESSION position.

http://php.net/manual/en/tutorial.forms.php
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month19 days, 7 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question