php 在不丢失 Post 值的情况下刷新页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12234545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refresh page without losing the Post value
提问by dames
How do I maintain the $post value when a page is refreshed; In other words how do I refresh the page without losing the Post value
刷新页面时如何维护 $post 值;换句话说,如何在不丢失 Post 值的情况下刷新页面
采纳答案by Glyn Hymanson
This in not possible without a page submit in the first place! Unless you somehow submitted the form fields back to the server i.e. Without Page Refresh using jQuery etc. Somesort of Auto Save Form script.
如果没有页面提交,这是不可能的!除非您以某种方式将表单字段提交回服务器,即不使用 jQuery 等进行页面刷新。某种自动保存表单脚本。
If this is for validation checks no need for sessions as suggested.
如果这是用于验证检查,则不需要按照建议进行会话。
User fills in the form and submits back to self Sever side validation fails
用户填写表单并提交回自我服务器端验证失败
$_GET
$_GET
<input type="hidden" name="first"
value="<?php echo htmlspecialchars($first, ENT_QUOTES); ?>" />
validation message, end.
验证消息,结束。
alternatively as suggested save the whole post in a session, something like this, but again has to be first submitted to work....
或者按照建议将整个帖子保存在会话中,类似这样,但再次必须首先提交工作....
$_POST
$_POST
if(isset($_POST) & count($_POST)) { $_SESSION['post'] = $_POST; }
if(isset($_SESSION['post']) && count($_SESSION['post'])) { $_POST = $_SESSION['post']; }
回答by ronalchn
You can't do this. POST variables may not be re-sent, if they are, the browser usually does this when the user refreshes the page.
你不能这样做。POST 变量可能不会重新发送,如果是,浏览器通常会在用户刷新页面时执行此操作。
The POST variable will never be re-set if the user clicks a link to another page instead of refreshing.
如果用户单击指向另一个页面的链接而不是刷新,则永远不会重新设置 POST 变量。
If $postis a normal variable, then it will never be saved.
如果$post是普通变量,则永远不会保存。
If you need to save something, you need to use cookies. $_SESSIONis an implementation of cookies. Cookies are data that is stored on the user's browser, and are re-sent with every request.
如果您需要保存某些内容,则需要使用 cookie。$_SESSION是 cookie 的实现。Cookie 是存储在用户浏览器上的数据,每次请求时都会重新发送。
Reference: http://php.net/manual/en/reserved.variables.session.php
参考:http: //php.net/manual/en/reserved.variables.session.php
The $_SESSION variable is just an associative array, so to use it, simply do something like:
$_SESSION 变量只是一个关联数组,因此要使用它,只需执行以下操作:
$_SESSION['foo'] = $bar
回答by alfasin
You can use the same value that you got in the POSTinside the form, this way, when you submit it - it'll stay there.
您可以使用在POST表单内部获得的相同值,这样,当您提交时 - 它会留在那里。
An little example:
一个小例子:
<?php
$var = mysql_real_escape_string($_POST['var']);
?>
<form id="1" name="1" action="/" method="post">
<input type="text" value="<?php print $var;?>"/>
<input type="submit" value="Submit" />
</form>
回答by WatsMyName
put post values to session
将帖子值放入会话
session_start();
$_SESSION["POST_VARS"]=$_POST;
and you can fetch this value in another page like
你可以在另一个页面中获取这个值,比如
session_start();
$_SESSION["POST_VARS"]["name"];
$_SESSION["POST_VARS"]["address"];
回答by WatsMyName
You could save your $_POST values inside of $_SESSION's Save your all $_POST's like this:
您可以将您的 $_POST 值保存在 $_SESSION 的保存所有 $_POST 中,如下所示:
<?php
session_start();
$_SESSION['value1'] = $_POST['value1'];
$_SESSION['value2'] = $_POST['value2'];
// ETC...
echo "<input type='text' name='value1' value='".$_SESSION['value1']."' />";
echo "<input type='text' name='value2' value='".$_SESSION['value2']."' />";
?>
回答by Cloudx
You can use file to save post data so the data will not will not be removed until someone remove the file and of-course you can modify the file easily
您可以使用文件来保存发布数据,因此在有人删除文件之前不会删除数据,当然您可以轻松修改文件
if($_POST['name'])
{
$file = fopen('poststored.txt','wb');
fwrite($file,''.$_POST['value'].'');
fclose($file);
}
if (file_exists('poststored.txt')) {
$file = fopen('ipSelected.txt', 'r');
$value = fgets($file);
fclose($file);
}
so your post value stored in $value.
所以你的 post 值存储在$value.
回答by Hemantha
Actually in html forms it keeps post data. this is valuble when you need to keep inserted data in the textboxes.
实际上在 html 表单中它保留发布数据。当您需要在文本框中保留插入的数据时,这是很有价值的。
<form>
<input type="text" name="student_name" value="<?php echo
isset($_POST['student_name']) ? $_POST['student_name']:'';
?>">
</form>

