簡體   English   中英

在PHP中顯示錯誤消息后如何停止表單重定向?

[英]How to stop a form redirection after displaying an error message in PHP?

在以下情況下,如何避免重新提交表單-我的HTML文件具有一個表單,該表單會將數據發送到自身進行驗證。 同一頁面中的<!DOCTYPE html>標記之前是必需的PHP語句。 這將獲取名為process.php的文件的內容,並檢查用戶數據。 如果數據不正確,則成功顯示錯誤消息。 如果數據正確,它將發送電子郵件。 -一切正常,但是如果用戶返回“按向后箭頭”並刷新頁面,則要求重新發送數據。 我如何避免這個問題。

<?php require("inc/process.php"); end(); ?>
<!DOCTYPE html>
<html>
 <head>
 <title>Page one</title>
 </head>
 <body>

   <div class="form-box">

   <!--Display error message PHP-->
   <?php
    if (isset($error_msg)) {
     $show_error = "<p class='error-message'>" . $error_msg . "</p>";
     echo strip_tags($show_error, '<p>');
    }
    ?>

    <form method="post" action="index.html">
     <input name="full-name" type="text">
     <input name="email" type="email">
    </form>

   </div> 


 </body>
</html>

最好使用jquery ajax,例如:

$("#FormID").submit(function(e) {

var url = "path/to/your/script.php"; // the script where you handle the form input.

$.ajax({
       type: "POST",
       url: url,
       data: $("#FormID").serialize(), // serializes the form's elements.
     })

  // when the request is succesfull
  .done(function( data ) {
    alert(data); // show response from the php script.
  })

  // when an error occurs
  .fail(function() {
    alert( "error" );
  });

e.preventDefault(); // avoid to execute the actual submit of the form. (no redirection)

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM