Jump to content

How to keep the previous data??


evon83

Recommended Posts

Hello there!

i know this sounds too eay for you guys but i still couldnt solve the problems!

I have this form fillin....
and when user press the enter button,
and the php code will runs the error checking..

if there's errors, messages will be displayed and ask them to go back...
however, how to keep the previous data that they have entered???

i know if users have to repeat key in the same thing again and again, that would drive them nuts...

please help!
and provide some sample code!

thanks!
Link to comment
Share on other sites

You dont send it to another script to validate - do this in the same script, here is a basic example of how it can be done
[code]

<?php

if(isset($_POST['submit']))
{

if(!empty($_POST['email']))
{
  // ok
  echo "Thank you!";
  $done = 1; // done is set, hiding form
}
else
{
  // email missing
  echo "Please fill inn required fields";
}

}

if(!$done)
{
echo <<<__HTML

<form method="post" action="this_page.php">

<input type="text" name="email" value="$email" />

<input type="submit" value="Do send" name="submit" />

</form>

__HTML;
}

?>
[/code]
Link to comment
Share on other sites

a) why do you want it in 2 pages?

b) the above code assumes register globals is set to ON. in the form, use value='{$_POST['email']}'  instead of $email.

c) if you really want to do it in 2 pages, you're going to have to
  1) make a cookie (user dependant)
  2) start a session (best option)
  3) store the information in a db or flatfile (not really convenient)

to make a session:

at the beginning of both of your scripts, put

session_start();

this must be before anything else (except your <?php tag)

then in your form, use $_SESSION['email'] as the input value for the email

in your processing script, set $_SESSION['email'] = $_POST['email'] if there is an error, kick them back to the form with a header or something. 
Link to comment
Share on other sites

This is my sample code:

Page1:

<?php
   
session_start();
?>

<form action="session2.php?" method="Post">
        <table align="center" border=0 cellspacing=5 cellpadding=0 width="100%">


        <table>
          <tr>
        <th align="left">Address :</th>
              <td><input type="text" size = "55" name="address1"/></td>
              <td><input type="text" size = "55" name="address2"/></td>
          </tr>
        </table>
        <center><input type="submit" name="submit" value="Submit"><input type="reset" Value="Reset"/></center>
        </form>


page2:
<?php
// page1.php

session_start();

$_session['address1']=$_POST['address1'];
$_session['address2']=$_POST['address2'];

echo "1:".$_session['address1'];
echo "<br>2:".$_session['address2'];

session_destroy();

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

my question is:
after i have clicked on page2....i wanna go back to page1, so click on the back button...
and when i do that, the data that i have keyed in previously has been erased automatically....
how to keep my previous data??

please help!

thanks!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.