[英]How do I have a web page store changes made by javascript?
I have a form on a simple web page that has a series of checkboxes. 我在具有一系列复选框的简单网页上有一个表单。 These options represent items that may be retrieved from a database upon clicking the submit button.
这些选项表示单击提交按钮后可以从数据库检索的项目。 In addition, there is a place to add an extra option to the page at the bottom.
此外,在底部的页面上还可以添加一个附加选项。 Here is what I am going for:
这是我要的:
I would like to be able to add the new option using javascript, so that it show up quickly and seamlessly to the user, rather than having to send the request back to the server and add it server-side, then have the user reload the page. 我希望能够使用javascript添加新选项,以便它可以快速无缝地显示给用户,而不必将请求发送回服务器并在服务器端添加它,然后让用户重新加载页。 However, I also want to make sure that these added fields are preserved so that the next time I load the page, they show up.
但是,我还想确保保留这些添加的字段,以便下次我加载页面时,它们会显示出来。
Unfortunately, my code is proprietary, so I cannot post it here. 不幸的是,我的代码是专有的,因此无法在此处发布。 I hope that people can help me this some ideas without having to actually see the code.
我希望人们可以在不必实际查看代码的情况下为我提供一些帮助。
Thanks 谢谢
When these checkboxes are added you don't want to go the server but when user presses submit you are anyways going to server, so at that time you can persist this information about new checkboxes on server. 添加这些复选框后,您不想进入服务器,但是当用户按下“提交”按钮时,您还是要进入服务器,因此在那时,您可以将有关新复选框的信息保留在服务器上。
Another option is to call to server asynchronously using AJAX to update the server about the state change. 另一个选择是使用AJAX异步调用服务器以更新服务器有关状态更改的信息。
If you still want to store the changes server-side, you can do so quietly in the background. 如果仍要在服务器端存储更改,则可以在后台安静地存储。
Just use XmlHttpRequest()
, together with a PHP script. 只需将
XmlHttpRequest()
与PHP脚本一起使用即可。
您可以使用HTML5本地存储API来存储您的更改。
localStorage.setItem('favoriteflavor','vanilla');
If you want session related information to pass thru multiple requests, you can either put the data in a cookie, or with php you can use sessions. 如果您希望会话相关信息通过多个请求传递,则可以将数据放在cookie中,也可以使用php使用会话。
http://php.net/manual/en/features.sessions.php http://php.net/manual/en/features.sessions.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.