php 即使在刷新页面后如何记住表单中的输入数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7014146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:48:29  来源:igfitidea点击:

how to remember input data in the forms even after refresh page?

php

提问by sasori

what to do in order to make the form remember the previous input or the current input of the user even after he/she refreshed the page ?

即使在用户刷新页面后,如何才能使表单记住用户的先前输入或当前输入?

should I do ,

我该不该做

            <div class="row">
             <label>Contact Messenger </label>
             <input type="text" name="messenger" id="messenger" size="50" maxlength="50" value="<?php echo $_SESSION['messenger']; ?>"/>
            </div>

or

或者

            <div class="row">
             <label>Contact Messenger </label>
             <input type="text" name="messenger" id="messenger" size="50" maxlength="50" value="<?php echo $_POST['messenger']; ?>"/>
            </div>

or

或者

there's a trick to do it ?..how ?

有一个技巧可以做到吗?...怎么做?

采纳答案by Khawer Zeshan

on the page where your form is submitting do something like this

在您提交表单的页面上执行以下操作

    session_start();
    $_SESSION['data'] = $_POST['data'];
    $_SESSION['data_another'] = $_POST['data_another'];

and than you can access those session variables any where like this

然后你可以在任何地方访问这些会话变量

    session_start(); // this should be at the top of the page before any html load
    <input type="text" name="name" value="<?php echo $_SESSION['data'];?>"/>

refresh your page on success call like this

像这样在成功调用时刷新您的页面

     $.ajax({
           type: "POST",
           url: "yourfile.php",
           data: 'data='+ data,
           success: function(){
               location.reload();
           }
       });

回答by arthurr

I had similar issue at one of my project so I wrote some js to handle this using cookies. First I found two simple functions to set and get cookies values:

我在我的一个项目中遇到了类似的问题,所以我写了一些 js 来使用 cookie 来处理这个问题。首先,我找到了两个简单的函数来设置和获取 cookie 值:

function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value;
}

function getCookie(c_name) {
    var c_value = document.cookie;
    var c_start = c_value.indexOf(" " + c_name + "=");
    if (c_start == -1) {
        c_start = c_value.indexOf(c_name + "=");
    }
    if (c_start == -1) {
        c_value = null;
    } else {
        c_start = c_value.indexOf("=", c_start) + 1;
        var c_end = c_value.indexOf(";", c_start);
        if (c_end == -1) {
            c_end = c_value.length;
        }
        c_value = unescape(c_value.substring(c_start, c_end));
    }
    return c_value;
}

Than I wrote two other functions to set and get input values:

比我写了另外两个函数来设置和获取输入值:

function saveValue(input) {
    var name = input.attr('name');
    var value = input.val();
    setCookie(name,value);
}

function getValue(input) {
    var name = input.attr('name');
    var value = getCookie(name);
    if(value != null && value != "" ) {
        return value;
    }
}

And using jQuery I save user's input value to session cookie

并使用 jQuery 我将用户的输入值保存到会话 cookie

$('input[type=text]').each(function(){
    var value = getValue($(this));
    $(this).val(value);
}).on('blur', function(){
    if($(this).val() != '' ) {
        saveValue($(this));
    }
});

I hope it'll help ;)

我希望它会有所帮助;)

回答by Chronial

That quite depends on where the data in the fields is coming from. If your scenario is that you are presenting a blank form, the user enters some data and then without submitting it, refreshes the page, there is nothing you can do. That's up for the browser to handle.

这在很大程度上取决于字段中的数据来自何处。如果你的场景是你呈现一个空白表单,用户输入一些数据,然后没有提交,刷新页面,你无能为力。这由浏览器来处理。

You might be able to do some very weird JavaScript hacking there, but I would not suggest that.

您可能可以在那里进行一些非常奇怪的 JavaScript hacking,但我不建议这样做。

On the other hand, if your data is coming from a previous page or something like that, that's a different story. But then you need to tell us where the data is coming from, so I can give you a reasonable answer.

另一方面,如果您的数据来自上一页或类似内容,那就是另一回事了。但是你需要告诉我们数据是从哪里来的,这样我才能给你一个合理的答案。

回答by Brad

If you mean, filling out form fields and clicking refresh, then there is no direct way, as the data isn't submitted. That sort of behavior is up to the browser.

如果您的意思是填写表单字段并单击刷新,则没有直接的方法,因为数据未提交。这种行为取决于浏览器。

The only thing you could do is post the data via AJAX back to your server as the fields change. Your PHP script receiving the data would set some session variables. When the form loads, you would set their default values to the appropriate session values.

您唯一能做的就是在字段更改时通过 AJAX 将数据发布回您的服务器。接收数据的 PHP 脚本会设置一些会话变量。当表单加载时,您可以将它们的默认值设置为适当的会话值。

Rarely though is there a need for this kind of behavior. There will be many requests sent back and forth to your server.

虽然很少需要这种行为。会有很多请求来回发送到您的服务器。

回答by Igor Parra

None of your suggestions will help you "remember" data after a refresh (action that not send data to the server). What you need is to grab data via javascript (maybe with onkeyup events type) and grab it using cookies. With jquery is very easy.

您的任何建议都不会帮助您在刷新后“记住”数据(不将数据发送到服务器的操作)。您需要的是通过 javascript(可能使用 onkeyup 事件类型)获取数据并使用 cookie 获取它。使用 jquery 非常容易。

回答by T.Todua

try the attribute autocomplete, maybe it will work on some browsers...

试试这个属性autocomplete,也许它可以在某些浏览器上工作......

<input type="text" autocomplete="on" />

You can use autocomplete for a formtoo.

您也可以使用自动完成功能form

回答by Rotem

There are some main issues to check in order to get the data appear after refresh or any event handler:

为了在刷新或任何事件处理程序后显示数据,需要检查一些主要问题:

1) Be aware of the signature. it must contain the method="post" attribute as follows:

1)注意签名。它必须包含 method="post" 属性,如下所示:

    <form method="post"/>

else the using of the $_POST['messenger'] will not work.

否则 $_POST['messenger'] 的使用将不起作用。

The default value of the method attribute in the form is get.

表单中method属性的默认值为get。

    <form>  equals to  <form method="get">

2) Many programmers mix between the attributes idvs name. Be aware of using the correct attribute. The $_POST associative array is using the nameto get data from it. That is why the next example will not workcorrectly:

2)许多程序员在属性idname之间混合使用。注意使用正确的属性。$_POST 关联数组正在使用名称从中获取数据。这就是为什么下一个示例无法正常工作的原因

    <input type="text" id="data" value="<?php echo isset($_POST['data']) ? $_POST['data'] : "" )?/>"

To get it work right you must add the attribute name="data" as follows:

要使其正常工作,您必须添加属性 name="data" 如下:

    <input type="text" name="data" id="data" value="<?php echo isset($_POST['data']) ? $_POST['data'] : "" )?/>"

Good luck

祝你好运

回答by Ali Abdul

You could also do without session like the below code:

你也可以像下面的代码一样没有会话:

<input type="text" name="Fname" value="<?php if(isset($_POST['Fname'])){ echo $_POST['Fname']; } ?>" /><br/>

This will keep the typed data inside the input box!

这会将输入的数据保留在输入框中!

回答by Hiroshi Rana

Session is a good way to store and remember data but why complicate things? Consider if the form has many fields, developer has to store lots of values in session. Why not simply print or echo.

Session 是一种存储和记忆数据的好方法,但为什么要把事情复杂化呢?考虑如果表单有很多字段,开发人员必须在会话中存储大量值。为什么不简单地打印或回显。

Example:

例子:

Form is submitted with input ssName and hidden field ssAct with value of send

使用输入 ssName 和隐藏字段 ssAct 提交表单,其值为 send

Get the ssName and ssAct on form submit

获取表单提交时的 ssName 和 ssAct

$ssAct=$_REQUEST["ssAct"];
$ssName=$_REQUEST["ssName"];

and the form

和表格

<input type="text" name="ssName" value="<?php if($ssAct=='send' && $ssName!='') { echo "$ssName"; } ?>">
<input type="hidden" name="ssAct" value="send">

On submit name will be echoed if it was submitted and was not empty.

如果提交名称不为空,则提交名称将被回显。