0

我正在使用 post 方法测试 Html 表单并得到了这个奇怪的结果:我在服务器 apache上有两个 html 页面(已经安装了 php):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 页面,并且目标页面的内容(假设名称 target.php)仍然存在(不像上面的 html 文件那样为空白) 那么,你能解释一下这个问题吗?谢谢你。

4

4 回答 4

1

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

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

于 2012-08-31T06:22:11.440 回答
1

这是因为您无法将输入从 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

于 2012-08-31T09:06:41.940 回答
0

首先,我将从更正您的 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>

这可能无关紧要,但您应该以 ; 结束所有样式。

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

于 2012-08-31T06:24:11.603 回答
0

将表单页面保存在 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>
于 2012-09-02T05:54:12.500 回答