Html Post方法在刷新后获得空白页


Html Post Method get blank page after refresh

我正在使用post方法测试Html表单,结果很奇怪:我在服务器apache(已经安装php)上有两个html页面:post.html和target.html,这些页面的代码如下:

post.html(不用担心html标准)

    <div style="text-align: center">
       <form method="POST" action="target.html">
          <input type="text" name="testname" value="sometext" />
          <input type="submit" value="submit" />
        </form>
    </div>

target.html

<html>
  <head>
  </head>
  <body>
    <h1>Target page</h1>
  </body>
</html>

当我在post.html页面上的表单中输入数据并点击提交按钮时,我到达了target.html页面。在这个页面(target.html)上,我刷新了页面,收到的是一个空白页面。第二次刷新时,它转到了正常的Html页面。

我不知道为什么它在我第一次刷新时返回了一个空白页面,我尝试过同样的方法,但在PHP页面上,目标页面(assum name target.PHP)的内容仍然保留(不像上面的html文件那样是空白的)那么,你能给我解释一下这个问题吗?非常感谢。

这肯定与您的浏览器有关。在使用Safari的mac上也是如此,在提交内容后的一些页面上,页面似乎冻结了,我刷新了它,然后它又工作了。

就我而言,这绝对不是一个代码问题。

这是因为您无法将输入从html传递到html文件。您的target.html应该是一个php文件(target.php)。并尝试将此代码放在您的target.php 上

<?php
var_dump($_POST); //this will show the inputted text and also show the data type and will stop the code execution here. other codes below will not be executed.
echo $_POST['testname'];
?>

此外,在表单操作中将target.html更改为target.php

首先,我将更正您的post.html

<div style="text-align: center;"> <!-- added a ; after center -->
   <form method="POST" action="target.html">
      <input type="text" name="testname" value="sometext" />
      <input type="submit" value="submit" />
    </form>
</div>

这也许并不重要,但你应该以结束你所有的风格;

继续说,一切看起来都很好。也许在两次刷新之间发生了一些奇怪的事情。

将表单页面保存在php中,而不是在同一页面中添加php脚本,请尝试此操作。记住保存在php.中

<?php $testname = $_Post['testname'];
echo $testname ?>

<div style="text-align: center">
       <form method="POST" action="">
          <input type="text" name="testname" value="sometext" />
          <input type="submit" value="submit" />
        </form>
    </div>