How to restore form data?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
webbiz
Forum Newbie
Posts: 22
Joined: Sat Mar 20, 2010 12:04 am

How to restore form data?

Post by webbiz »

Using a simple form in a file called test.php, the user enters:

name
address
city
state
country

in the appropriate textboxes and hits the SUBMIT button.

This opens a new php script called addr_info.php shown below:

----------------------------

Code: Select all

<?php 

session_start();

$name = $_POST['name'];
$address1 = $_POST['address1'];	
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$postalcode = $_POST['postalcode'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$country = $_POST['country'];

echo "<hr />";
echo "CUSTOMER INFORMATION PROVIDED<br /><br />";
echo "<strong>$name</strong> <br />";
echo "<strong>$address1</strong> <br />";

if (empty($address2) == FALSE) 
	echo "<strong>$address2</strong> <br />";
echo "<strong>$city, $state</strong> <br />";
echo "<strong>$country</strong><br />";
echo "<strong>$postalcode</strong> <br /><br />";
echo "<strong>$phone</strong> <br />";
echo "<strong>$email</strong> <br />";

echo '<br /><a href="orderform_shipping.php">EDIT</a>';
echo "<hr />";


?>
------------------------------------

The session variables are displayed and this now works fine (thanks to Greg in another thread).

Now, suppose that the user realizes that he entered one or more values incorrectly. I provided a LINK that the user can click on to return to the form, but the textboxes are all clear.
So I tried some things I found on the net, like:

Code: Select all

<input name="name" type="text" id="name"  value="<?=$_REQUEST['name']?>"/>
But this did not work. I also tried:

Code: Select all

<input name="name" type="text" id="name"  value="<?php echo $name; ?>"/>
But this did not work either.

I sure could use some advice on how to return to the form in order to make changes without having to type everything over again.

Thanks.
Last edited by Benjamin on Tue Oct 26, 2010 5:52 pm, edited 1 time in total.
Reason: Removed ALL CAPITAL title. Added [syntax=php] tags.
webbiz
Forum Newbie
Posts: 22
Joined: Sat Mar 20, 2010 12:04 am

Re: How to restore form data?

Post by webbiz »

While waiting to see if anyone knew how to do this, I did a bit of experimenting. As a newbie to PHP, I can't say if this is the best way to go, but as I'm trying to avoid Javascript because it can be turned off by the user, here is one approach I did that seems to work.

When my form is submitted, another .php script is called (method=POST).

It displays the form information and then puts a link at the bottom of the page to allow the user to click and return to the form if any of the data is incorrect. This allows the user to edit what was submitted in the form.

My problem as previously stated was that I needed the original data to show up in the form so the user did not have to type it all in again.

To accomplish this, I decided to create the EDIT link that returns the user to the original form page using this code:

$urlstring = "orderform_shipping.php?name=" .$name. "&address1=" .$address1. "&address2=" .$address2. "&city=" .$city. "&state=" .$state. "&postalcode=" .$postalcode. "&phone=" .$phone. "&email=" .$email. ">EDIT</a>";

$urlstring = str_replace(" ", "&nbsp;", $urlstring);

echo "<br /><a href=" .$urlstring;

This created a link called EDIT with a URL that included all the form info correctly formatted.

Then on the form itself, I only had to add "value=" to each textbox with the $_GET['var']; (ex: $_GET['name']).

It's not perfect, but at least it works. 11 people viewed my problem without a solution posted, so I have to imagine this isn't an easy issue for most. But if there is a cleaner, better way, I'd appreciate learning about it.

Thanks.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: How to restore form data?

Post by s.dot »

If you can POST the form to the same page, it would be easy to set the entered data, such as

Code: Select all

<input type="text" name="name" value="<?php echo !empty($_POST['name']) ? $_POST['name'] : ''; ?>"
If you must (or just want to) post to a different processing script than where the form is located, you can store the values in a session, and then set the values like above using the $_SESSION superglobal.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
webbiz
Forum Newbie
Posts: 22
Joined: Sat Mar 20, 2010 12:04 am

Re: How to restore form data?

Post by webbiz »

I've been racking my brains over this session superglobal variables thing. Surfed the net and read a ton, but the examples aren't gelling for me.

FORM1.php takes only shipping information and no credit card info.

When the user clicks SUBMIT, I want a new page (PAGE2.php) to open showing what he has entered. If okay, the billing box (for credit card info) is also on that page to be filled out.

However, if the shipping info needs editing, I want a link back to the FORM1 so the user can correct the entry.

I'm a bit puzzled as to WHERE and HOW to get the form data into the session superglobal variables. Clearly this has to be AFTER the user enters the info into the textboxes. How does the form know? I'm assuming by the SUBMIT button.

So is it the PAGE2.php where I would do this? And if so, how is this done?

Right now, in PAGE2.php, I do something like this:

$name = $_POST['name'];

Should I then do something like this right after the above?

$_SESSION['name']=$name

And if so, then on FORM1.php, would I add "value=<?php echo $_SESSION['name'];?>" for the textbox?

Hope my question is clear.

Thanks!
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: How to restore form data?

Post by s.dot »

Hello, do something like this..

Page 1 (the form)

Code: Select all

<php
session_start();
?>

<form action="page2.php" method="post">
<input type="text" name="name" value="<?php echo !empty($_SESSION['name']) ? $_SESSION['name'] : ''; ?>">
<input type="submit" value="submit">
</form>
Page 2 (the processing script)

Code: Select all

<?php
session_start();

if (!empty($_POST))
{
    if (!empty($_POST['name']))
    {
        $name = $_POST['name'];
    } else
    {
        die('please enter your name');
    }

    //after you have declared/checked all your variables, enter them into the session
    $_SESSION['name'] = $name;

    echo '<a href="page1.php">go back to page one with name filled in</a>';
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
webbiz
Forum Newbie
Posts: 22
Joined: Sat Mar 20, 2010 12:04 am

Re: How to restore form data?

Post by webbiz »

Thank you s.dot.

That approach appears the way to go.

:)
Post Reply