简体   繁体   English

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

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

for example the following file is SamePage.php例如以下文件是 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>

It's not very nice to have the exit there and sometimes I don't want to refresh the page, but it has to be done encase the user presses the refresh on the browser and the same POST data gets sent again and things get messed up.在那里有出口不是很好,有时我不想刷新页面,但必须这样做,以防止用户在浏览器上按下刷新,并且再次发送相同的 POST 数据并且事情变得一团糟。 Can anyone think of a better way?谁能想到更好的方法?

Best is to send the form to another page and the reload your page... But if you don't want, you can:最好是将表单发送到另一个页面并重新加载您的页面......但如果您不想要,您可以:

  • Use AJAX使用 AJAX
  • Put all your HTML code between { } symbols将所有 HTML 代码放在 { } 符号之间
  • Use templates (avoid mix PHP and HTML)使用模板(避免混合 PHP 和 HTML)

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

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