简体   繁体   English

页面刷新后如何保留提交的表单数据?

[英]How can I keep submitted form data after a page refresh?

Currently I have a page designed to automatically refresh after an interval of 10 seconds using the code: 目前,我有一个页面设计为使用代码间隔10秒后自动刷新:

<meta http-equiv="refresh" content="10" />

I also have a series of drop down menus so a user can select various elements, which will return a set of values I have stored in a database. 我还具有一系列下拉菜单,因此用户可以选择各种元素,这些元素将返回我存储在数据库中的一组值。 The issue is after a form post, the data returned from the database is cleared from the browser after a refresh. 问题出在表单发布之后,刷新后从浏览器清除了从数据库返回的数据。

Is there a way to keep that data displayed even after an auto refresh? 有没有办法即使在自动刷新后仍保持该数据显示?

This is my page: http://esp.southhills2013.info/php_test.php 这是我的页面: http : //esp.southhills2013.info/php_test.php

使用ajax为用户保存选择的值,然后在用户访问此页面时使用此保存的值

Instead of auto-refreshing the entire page, try using AJAX to just reload the parts of the page that need updating. 与其自动刷新整个页面,不如尝试使用AJAX重新加载页面中需要更新的部分。

You can probably even hack around this in jQuery by just sending an AJAX request for the current page, then getting the updated parts from the HTML and using .replaceWith() to update them on the page. 您甚至可以通过仅发送当前页面的AJAX请求,然后从HTML获取更新的部分并使用.replaceWith()在页面上进行更新,甚至在jQuery中解决该问题。

Depending the size and type of the data you could use a cookie or the session. 根据数据的大小和类型,您可以使用cookie或会话。 If none of that is possible you can write the data to a file. 如果这都不可行,则可以将数据写入文件。

the best way to achieve this is by using SESSIONS 最好的方法是使用SESSIONS

for example in php 例如在PHP

<?php
session_start();
if(!empty($_POST['submit']))
{
  $_SESSION=$_POST;
}
?>

and in form 并以形式

<select name="day">
<option value="1" selected="<?php $_SESSION['day']==1?"selected":''?>">1</option>
<option value="2" selected="<?php $_SESSION['day']==2?"selected":''?>">2</option>
.....
</select>
<!--or you can print in php using a loop-->

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

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