后重定向 - 获取(PHP)代码麻烦

时间:2018-03-17 13:25:45

标签: php post-redirect-get

我有这个简单的PHP代码......

<html>
 <head>
 </head>
 <body>
  <form name="test" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
   City name: <input type="text" name="city"><br>
   <input type="submit" name="submit" value="Search"><br>
  </form>

  <?php
   if(isset($_POST['submit']))
    {
     $city = $_POST['city'];

     echo "The typed city is: ".$city;

     header('Location: '.$_SERVER['REQUEST_URI']);
     exit();
    }
  ?>
 </body>
</html>

我添加了......

     header('Location: '.$_SERVER['REQUEST_URI']);
     exit();

..根据Avoiding form resubmit in php when pressing f5或此处Simple Post-Redirect-Get code example的建议来管理页面刷新,否则会出现一条消息,要求我确认重新发送并重复操作,而不是清除页面。

我已尝试使用'REQUEST_URI''PHP_SELF' ...结果是页面已刷新并清理但我在页面中看不到{{1}的结果指令......

我希望能够看到键入的城市名称然后,当我刷新页面时,清除所有内容,避免刷新任何消息

连连呢?提前谢谢

修改

我已经解决了以这种方式更改原始代码的问题(从那里开始...... https://www.canbike.org/information-technology/php-form-clear-data-on-refresh-or-back-button-press.html

第一个档案:NoRefreshForm.php

echo "The typed city is: ".$city;

第二档:NoRefreshCalculate.php

<?php
session_start();
$_SESSION['page1']=1;
?>

<html>
<body>
    <form name="test" action="NoRefreshCalculate.php" method="post">
        City name: <input type="text" name="city"><br>
        <input type="submit" name="submit" value="Search" />
    </form>
</body>
</html>

第三档:NoRefreshResults

<?php
session_start();
?>

<html>
<body>
    <?php
        if (isset($_POST['city']) && isset($_SESSION['page1']))
        {
            #Do calculation here. Store in $_SESSION.
            $_SESSION['page2']="2";

            $_SESSION['city']=$_POST['city'];
            header('Location: NoRefreshResults.php');
    }
        else
        {
            header('Location: NoRefreshForm.php');

        }
    ?>
</body>
</html>

通过这种方式,您可以在重新加载时提交文本,详细说明并刷新页面而不显示浏览器消息。我希望这可以帮助有同样问题的人

0 个答案:

没有答案