Home > php > Retaining Form Data

Retaining Form Data

I was looking to find a way in php to retain form data. After poking around the net through various tutorials, I found a way. I can script some basic things, but am still quite a novice at php.

So here’s what i found I could do, with the help of a blog entry at scriptygoddess.

For this, I will use a $_SESSION() to help carry the data throughout the pages. This requires that the following code be placed at the top of all the pages involved:

< ?php
session_start();
header("Cache-control: private"); // IE 6 Fix.
?>

Now we need the form page(with the above code in it too):

<form method="post" action="preview.php">
First Name:<input type="text" name="First_Name" value="<? print("$_SESSION[First_Name]");?/>" /><br />
Last Name:<input type="text" name="Last_Name" value="<? print("$_SESSION[Last_Name]");?/>" /><br />
<input type="submit" value="submit"/>
</form>

When submit is pushed, you can see the action takes us to a preview page(preview.php). This page will display what youve entered in the form, and give you the option to go back and edit your data, or agree, and continue processing the form.

< ?php
//preview.php

// Start session
session_start();
header("Cache-control: private"); // IE 6 Fix.

// actions to take from buttons
if (isset($_POST["goBack"])) { // Go Back and Edit
    header("Location: form.php");
} else {
    if (isset($_POST["Send"])) { // OR process the form
        header("Location: process.php");
    }
}

// Dynamically Assign Key & Value to each Post
foreach($_POST as $key=>$value){
    $_SESSION[$key]=$value;
}
?>

The key element to holding the data to fill back in the form with the data you entered lies in this bit of code:

// Dynamically Assign Key & Value to each Post
foreach($_POST as $key=>$value){
    $_SESSION[$key]=$value;
}

What this does is take all the data you entered “$_POST” and turn it into an array. It first assigns it a $key and also holds the value of it i.e. =>$First_Name. So in this example the $_POST array looks like:
array([0]=>what_you_entered_for_the_first_name[1]=>what_you_entered_for_the_last_name)

So when you hit ‘edit’ this will cause:

First Name:<input type="text" name="First_Name" value="<? print("$_SESSION[First_Name]");?/>" />

to display what was entered originally.

Categories: php Tags:
  1. June 2nd, 2010 at 14:42 | #1

    rtetert

  2. July 18th, 2010 at 23:38 | #2

    dadasdas

  3. January 15th, 2012 at 21:11 | #3

    I will be really thankful for the author on this post to make this lovely and informative article live to put us. We actually appreciate ur effort. Maintain the excellent work. .

  4. March 20th, 2012 at 23:51 | #4

    I want to thank you for this site to pay for female. I hope that the website in the upcoming high-grade secondary school to more skills, in fact, your creative skills and encouraged me to let my site has improved. In fact, the blog is spreading its wings rapidly. You write up is a good example.

  5. March 21st, 2012 at 01:00 | #5

    I have to point out my appreciation for your generosity supporting individuals that should have help with this one area. Your very own commitment to getting the solution all over ended up being incredibly insightful and has usually made many people just like me to realize their ambitions. Your entire interesting guide implies much to me and even more to my office workers. Thank you; from each one of us.I am happy that you just shared this useful information with us. Please stay us informed like this. Thank you for sharing.

  1. September 9th, 2005 at 10:27 | #1
  2. January 12th, 2009 at 07:59 | #2
  3. March 24th, 2010 at 09:18 | #3