繁体   English   中英

记住输入历史

[英]Remember input history

我正在使用使用流程页来验证然后处理所有信息的表单。 如果出现问题或消息丢失,则该消息会告诉用户返回页面并进行修复。 但是,当他们这样做时,他们输入的所有其他信息都不会显示在字段中。 我该怎么做,这样无论他们投入什么都可以展示。

一种简单的解决方案是在发布后将所有POST数据存储在SESSION中。

if($_POST){ // if we have any POSTed variables...
    $_SESSION['postdata'] = $_POST; // set a browser session with all of the values
}

然后,在表单页面上,检查postdata是否存在,如果存在,则填写输入值。

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'] .'" />';
}

像这样! 玩得开心!

最好的方法是在同一页面上进行验证,并且仅在成功之后才重定向到另一个页面:

<?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>

或者:使用会话。

使用javascript或ajax在同一页面中进行验证(如果需要在服务器端进行检查)

这样您就不会丢失任何数据

您可以在网上找到许多示例(寻找使用Ajax进行表单验证)

像这篇文章: http : //jqueryfordesigners.com/demo/ajax-validation.php

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM