如何使表单数据在刷新后不消失?
我去另一个浏览器上测试我的页面。在谷歌浏览器上,我可以填写表格,来回点击,并且仍然有数据。现在我需要刷新页面,以便某些数据正确(例如会话 ID,如果 cookie 过期或用户在提交之前注销)。我刷新并丢失所有数据。我可以设置一些选项来保留所有数据吗?
I went to test my page on another browser. On google chrome i can fill out a form, hit back and forward and still have the data there. Now i need to refresh the page so certain data is correct (such as session id if the cookie expires or user logs out before submitting). I refresh and lose all data. Is there some option i can set so all data is kept?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你使用什么框架?例如,ASP.Net WebForms 将通过 ViewState 处理此问题(恶心),ASP.Net MVC 将要求您手动执行此操作等。
您本质上需要在页面重新加载时将数据保留在某处,然后重新填充控件。
What framework are you using? For example, ASP.Net WebForms would handle this via ViewState (yuck), ASP.Net MVC would require you to do this manually etc.
You essentially need to persist your data somewhere while the page reloads, and then re-populate the controls.
您必须在输入值时将其发送到服务器,然后在刷新时重新填充表单字段。
You would have to send the values to the server while they are typed in, and then repopulate the form fields on refresh.
是的,唯一安全的方法是使用服务器端脚本临时存储表单。由于浏览器处理后退/前进的方式不同,如果您不使用服务器,您的页面将不兼容 x 浏览器。如果用户点击后退按钮,您就会迷失方向,因为没有发布任何内容,除非您在刷新新页面之前使用一些 javascript 魔法发布表单。
Yes, the only secure way to do this is to use a serverside script to store the form temporarly. Since browsers handles back/forward diffrently your page won't be x-browser compatible if you don't use the server. If the user hits the back button you are kind of lost already since no post is done, unless you post the form with some javascript magic before the new page is refreshed.