简体   繁体   English

在PHP页面上保存数据的最简单方法

[英]Easiest way to persist data on a PHP page

I a web application that I am developing (being developed in PHP) when a user posts any request for service the user has to fill in a very length form. 我正在开发一个Web应用程序(用PHP开发),当用户发布任何服务请求时,用户必须填写一个非常长的表单。

Once user fills in the form and submits it another page (confirmation page) is loaded in which all values filled in by user are show. 用户填写表单并提交后,将加载另一个页面(确认页面),其中显示用户填写的所有值。 I must point out that at this stage the data is not saved to MySQL yet. 我必须指出,在这个阶段,数据还没有保存到MySQL。 Now on this page the user can either save the service request or go back. 现在,在此页面上,用户可以保存服务请求或返回。

My problem starts here. 我的问题从这里开始。 When the user goes back . 当用户回去时 All data filled in is lost . 填写的所有数据都将丢失 BTW User tend to use Browsers Back button instead of the back button provided on the confirmation page. BTW用户倾向于使用浏览器返回按钮而不是确认页面上提供的后退按钮。

How can is persist data filled in by user so that user can edit/make changes to data and again submit changed data. 如何通过用户填写持久数据,以便用户可以编辑/更改数据并再次提交更改的数据。

If you aren't persisting it to the DB yet, one thing you could do is potentially store the values in the user session. 如果您还没有将其持久化到数据库,那么您可以做的一件事是将值存储在用户会话中。 So when the user goes forward, save the information into the session, and then when they hit back, load the information from the session. 因此,当用户前进时,将信息保存到会话中,然后当他们回击时,从会话中加载信息。 Then when they are finally sure with the dat, you can send it to the DB. 然后,当他们最终确定数据时,您可以将其发送到数据库。

u can use Code: 你可以使用代码:

`session_cache_limiter('must-revalidate');`

That works well for me. 这对我很有用。 Keep in mind this line must go before session_start(). 请记住,此行必须在session_start()之前。 Anyway, this is useful for me because I don't have to deal with all the session variables that comes with sessions. 无论如何,这对我有用,因为我不必处理会话附带的所有会话变量。

OR put the form data into a session like so: Code: 或者将表单数据放入如下的会话中:代码:

$_SESSION['myFormData'] = $_POST;

Probably the easiest way would be to store the data in the session. 可能最简单的方法是将数据存储在会话中。 Have your data entry form check the session to see if the data is there, and if it is, pre-populate the form with that data. 让您的数据输入表检查会话以查看数据是否存在,如果存在,则使用该数据预填充表单。

Once they complete the process and the data is stored in the DB, clear those values from the session. 完成此过程并将数据存储在数据库中后,从会话中清除这些值。

Using cookies and JavaScript you could: 使用cookie和JavaScript您可以:

  1. store all the values in a cookie when the form is submitted 在提交表单时将所有值存储在cookie中
  2. use the onload handler to check for the cookie and populate the form with the data found in the cookie 使用onload处理程序检查cookie并使用cookie中的数据填充表单

Don't forget to code it in a way that allows users without cookies or JavaScript to use the page; 不要忘记以允许没有cookie或JavaScript的用户使用该页面的方式对其进行编码; they just don't get to have the form populated for them. 他们只是没有为他们填充表格。

Every once in a while someone comes up with a good solution. 每隔一段时间,有人会提出一个很好的解决方案。 This is that time. 就是那个时候。

HTML 5 offline storage is supported now in all major browsers since IE8. 自IE8以来,所有主流浏览器都支持HTML 5离线存储。 With that in mind, I decided to tackle form state once and for all so all scripting languages and even HTML pages can keep form state. 考虑到这一点,我决定一劳永逸地处理表单状态,因此所有脚本语言甚至HTML页面都可以保持表单状态。 No cookies needed, no memory-hogging sessions needed, no server-side programming needed and you can partially fill the form out go where you want to whatever URL, come back and the form is there where you left off. 不需要cookie,不需要内存占用会话,不需要服务器端编程,你可以将表单部分填写到你想要的任何URL,返回,表单就在你离开的地方。 Just drop some small script and you are done. 只需删除一些小脚本即可完成。

Here it is for free -> http://www.jasonsebring.com/dumbFormState 这是免费的 - > http://www.jasonsebring.com/dumbFormState

Let me know what you think. 让我知道你的想法。

Use sessions . 使用会话 They're very easy to use. 它们非常容易使用。

On your confirmation page you can put a form with hidden fields that contain all values and a Back submit button which will post them to the previous page. 在您的确认页面上,您可以放置​​一个包含所有值的隐藏字段的表单和一个将它们发布到上一页的“后退”提交按钮。 You could also have a second submit button on this form which will post to an url that will write the values to the database. 您还可以在此表单上有第二个提交按钮,该按钮将发布到将值写入数据库的URL。

I did a wizard a few years ago where one of the requirements was that the user should be able to go next/previous and even jump back and forth to steps that had been completed, so if the user had completed step 4 and jumped 2 steps back by clicking previous 2 times he/she should be able to click on step 4 on the "wizard progressor" and jump to step 4. For this I used Serializing objects - objects in sessions and stored the "wizard object" in a database, worked pretty good iirc. 几年前我做了一个向导,其中一个要求是用户应该能够下一个/上一个,甚至来回跳转到已完成的步骤,所以如果用户已完成步骤4并跳过两个步骤通过单击前面的2次,他/她应该可以单击“向导进度器”上的步骤4并跳转到步骤4.为此,我使用了序列化对象 - 会话中的对象并将“向导对象”存储在数据库中,工作得很好iirc。

EDIT: Ahh, yes. 编辑:啊,是的。 Another requirements was that the user should be able to return and complete the wizard several weeks later or continue an abandon wizard if there existed one for that users mail address. 另一个要求是用户应该能够在几周后返回并完成向导,或者如果存在该用户邮件地址,则继续放弃向导。

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

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