简体   繁体   English

提交之前存储表单数据的最佳方法?

[英]Best way to store forms data before submit?

First let me describe the scenario. 首先让我描述一下情况。 Suppose a user filling a form in my site. 假设一个用户在我的网站上填写了一个表格。 The form may contain around 14 fields. 该表格可能包含约14个字段。 Now after completing the 7fields he/she go back by mistake or close the window or the pc take a restart. 现在,在完成7个字段之后,他/她会错误地返回或关闭窗口,或者PC重新启动。

Now when he/she came back is there a way to show him that 7 fields filled up? 现在,当他/她回来时,有没有办法向他展示7个字段被填满? Now there is three case to handle: 现在有三种情况要处理:

Go back from the main page Close the window Take a pc reboot. 从主页返回关闭窗口重新启动PC。

There might be no solution for all of this. 所有这些可能都没有解决方案。 If only 1 and 2 situation can be handled that is fine. 如果只能处理1和2的情况,那就很好。

I know about session but so far i know to save something in session you need to submit the site or refresh which is not my considerable case. 我知道会话,但到目前为止,我知道要在会话中保存一些内容,您需要提交站点或刷新,这与我的情况无关。

So i need a good way to do that? 所以我需要一个很好的方法来做到这一点?

In actual my site will host around 20-40 fields in each forms. 实际上,我的网站将以每种形式托管大约20-40个字段。 So it is very important to make a recovery for the user if he close the tab or go back. 因此,如果用户关闭标签或返回,对用户进行恢复非常重要。

You can also set the session VIA ajax every x seconds. 您还可以每x秒设置一次VIA ajax会话。 That way you eliminate your problem about refreshing the page to save your session value. 这样,您就消除了有关刷新页面以保存会话值的问题。

setInterval(function(){
     $.post("/ajaxLogin.php",{
            value: $("#value").val()
        }, 
        function(result){
            console.log('saved!');
      });
}, 2000);

And this is the ajaxLogin.php that is called via ajax 这是通过ajax调用的ajaxLogin.php

<?php
    session_start();
    $_SESSION['value'] = $_POST["value"];
?>

In the web world, the usual thing to do is to either save data in Cookies or Sessions. 在网络世界中,通常要做的是将数据保存在Cookie或会话中。 Cookies are stored in the user's machine until its expiration date is met, while Sessions usually have a shorter time span (depends usually on the server's session time to live). Cookie会存储在用户的计算机中,直到满足其到期日期为止,而Session通常具有较短的时间跨度(通常取决于服务器的生存会话时间)。

One approach, if the user can leave and come back at a later point, (even after a restart), is to save it to a cookie. 如果用户可以离开并稍后再回来(即使在重新启动之后),一种方法是将其保存到Cookie中。 You can use setcookie in PHP for this, and serialize the data. 您可以为此在PHP中使用setcookie,并序列化数据。

Example: 例:

setcookie("UserFormData", serialize($_POST), time()+3600);  /* expire in 1 hour */

From: http://php.net/manual/en/function.setcookie.php 来自: http : //php.net/manual/zh/function.setcookie.php

您可以使用Ajax将数据保存到会话中...您无需刷新..您可以在每次字段更新时或需要时进行ajax调用。.ajax调用不需要等待表单提交。在每个按键/击键上触发(尽管不推荐)

You could create an onchange function that creates/updates a cookie . 您可以创建一个onchange函数来创建/更新cookie You can set up a specific cookie for each input, assuming you're using PHP and the function in the link I provided you could do: 您可以为每个输入设置一个特定的cookie,假设您使用的是PHP,而我提供的链接中的函数可以做到:

<input type="text" name="username" onchange="SetCookie('username',this.value,1);" value="<?=$_COOKIE['username'];?>" />

This seems like it would be very tedious, there may be a much better solution... 这似乎很繁琐,可能会有更好的解决方案...

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

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