【问题标题】:what's the best way to have a form on a page send information to the same page让页面上的表单将信息发送到同一页面的最佳方式是什么
【发布时间】:2011-07-17 00:26:55
【问题描述】:

例如下面的文件是 SamePage.php

    <!DOCTYPE html>
    <html lang="en">
    <head><title>Personal Information</title></head>
    <body>
    <?php
         if(!empty($_POST))
         {
              echo $_POST['var'].'<br />';
              //do a bunch of stuff
              ...
              echo "Inserted into database succesfully! You will now be redirected.<meta http-equiv=refresh content=4;url=database.php><br /></body></html>";//meta refresh gets rid of old POST data
              exit;
         }
     ?>
Add another entery:<br />
<form method="POST" action="SamePage.php"><--!It sends the information to itself-->
name: <input type="text" name="name" size="10" maxlength="25" /><br />
telephone: <input type="text" name="telephone" size="10" maxlength="25" /><br />
birthday: <input type="text" name="birthday" size="10" maxlength="25" /><br />
<input type="submit" value="add" />
<input type="reset" value="clear" />
</form>
</body>
</html>

在那里有出口不是很好,有时我不想刷新页面,但必须这样做,以防用户在浏览器上按下刷新,并且再次发送相同的 POST 数据并且事情得到弄乱。谁能想到更好的办法?

【问题讨论】:

  • 有时候页面不刷新是什么原因?

标签: php sql forms


【解决方案1】:

最好将表单发送到另一个页面并重新加载您的页面......但如果您不想要,您可以:

  • 使用 AJAX
  • 将所有 HTML 代码放在 { } 符号之间
  • 使用模板(避免混合使用 PHP 和 HTML)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    相关资源
    最近更新 更多