簡體   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