Remember input history
I am using a form that uses process page to verify and then proce开发者_高级运维ss all info. If something is wrong or is missing the message tells the users to go back to the page and fix it. However, when they do it, all other information that they put in is not shown in the fields. How can I make it so whatever they put in will show.
A simple solution would be to store all POST data inside of a SESSION upon posting it.
if($_POST){ // if we have any POSTed variables...
$_SESSION['postdata'] = $_POST; // set a browser session with all of the values
}
Then, on your form page, check if postdata exists, and if so, fill input values.
if($_SESSION['postdata']){ // if postdata exists
$p = $_SESSION['postdata']; // retrieve values in a new variable
unset($_SESSION['postdata']); // unset session, because you don't want this post data to travel all around your website
/* for each input or whatever you got there... */
echo '<input name="your-key-here" type="text" value="'. $p['your-key-here'] .'" />';
}
Something like that! Have fun!
The best way is to do the validation on the same page and only redirect to another page when that succeeded:
<?php
if (!empty($_POST['foo'])) {
// validate
if (/* valid */) {
// save data somewhere
header('Location: nextpage.php');
exit();
}
}
?>
<form action="samepage.php">
<input name="foo" type="text" value="<?php if (!empty($_POST['foo'])) echo htmlentities($_POST['foo']); ?>">
<?php if (/* foo was invalid */) : ?>
<p class="error">Please fill in Foo!</p>
<?php endif; ?>
</form>
Alternatively: use sessions.
make the validation in the same page with javascript or ajax(if you need to check in the server side)
then you don't lose any data
you can find in the web many examples (look for form validation with ajax)
like this article : http://jqueryfordesigners.com/demo/ajax-validation.php
精彩评论