【问题标题】:PHP Unset $_POST on page refreshPHP 在页面刷新时取消设置 $_POST
【发布时间】:2018-03-22 12:01:21
【问题描述】:

我试图阻止 php 保留我的表单提交,但遇到了困难。这是我开始的。

<?php
echo "<form action='' method='post'>
<input id='scan' class='scan' name='scan' type='submit' value='Scanning' />
</form>";

if(isset($_POST['scan'])){
echo "Very good!"
}
?>

我尝试了以下方法:

if(isset($_POST['scan'])){
echo "Very good!"
unset ($_POST);
}

if(isset($_POST['scan'])){
echo "Very good!"
unset ($_POST['scan']);
}

if(isset($_POST['scan'])){
echo "Very good!"
exit();
}

if(array_key_exists('scan',$_POST)){
echo "Very good!"
unset ($_POST);

}

我似乎无法弄清楚。我希望在刷新页面或单击后退箭头进入此页面时重置 $_POST。在这个例子中,我想要文本“非常好!”在页面刷新时不显示。

【问题讨论】:

  • Refreshing 将重新提交表单,所以它只是按照您的指示进行操作
  • @miknik 是否有任何变通方法使表单不会重新提交?
  • 使用 ajax 提交表单并保存访问者页面刷新

标签: php arrays variables isset unset


【解决方案1】:

正如 John Conde 所建议的,POST/REDIRECT/GET 模式会很有用。他分享的链接将为您提供良好的工作基础。你需要在不输出任何东西的情况下处理 POSTed 数据,then 重定向,then 输出。

您可以过度简化并将发布数据转储到会话变量并在重定向页面上处理它。

if($isset[$_POST['scan'])) {
    $_SESSION['post_data'] = $_POST;  
    header('Location: index.php', true, 303);
}

或者您可以在 POST 页面上完成所有繁重的工作,然后重定向到输出页面。

if($isset[$_POST['scan'])) {
    // do stuff with $_POST that doesn't output.  
    // Store results server-side with reference
    // like a $_SESSION or an id.
    header("Location: index.php?id=$id", true, 303);
}

【讨论】:

    猜你喜欢
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多