繁体   English   中英

让页面上的表单将信息发送到同一页面的最佳方式是什么

[英]what's the best way to have a form on a page send information to the same page

例如以下文件是 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 数据并且事情变得一团糟。 谁能想到更好的方法?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM