如何取消设置帖子数组? [英] how to unset post array?

查看:57
本文介绍了如何取消设置帖子数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次刷新页面时,都会得到存储在post数组中的相同值. 我只想在提交后和刷新后没有回声结果的情况下执行回声语句.

every time i am refreshing the page and i am getting the same value stored in the post array. i want execution of echo statement only after submit and after refreshing no echo results..

<?php
if(isset($_POST['submit'])) 
{ 
$name = $_POST['name'];
echo "User name : <b> $name </b>";


}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>

推荐答案

如果您提交表单,然后刷新结果页面,浏览器将重新发布该表单(通常先提示).这就是为什么POST数据始终存在的原因.

If you submit a form and then refresh the resulting page, the browser will re-post the form (usually prompts first). That is why the POST data is always present.

一种选择是存储一个会话变量,然后将其发送到表单中,然后检查它是否与表单处理代码匹配-确定是否是重新发布.

An option would be to store a session variable and have it sent in the form, then check if it matches in the form processing code - to determine if it is a re-post or not.

以以下形式:

<input type="hidden" name="time" value="<?php echo $time; ?>" />

在PHP中:

session_start();

if(isset($_POST['submit'])) 
{
    if(isset($_SESSION['time']) && $_SESSION['time'] == $_POST['time'])
    {
        echo "User name : <b> $name </b>";
    }
}

$time = $_SESSION['time'] = time();

另一种选择是在处理帖子数据后重定向:

Another option is to redirect after processing the post data:

if(isset($_POST['submit'])) 
{
    ...
    ...

    header('Location: ' . basename($_SERVER['PHP_SELF']));
    exit();
}

这篇关于如何取消设置帖子数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆