2017-03-05 77 views
1

表單提交後,表單未被驗證,並且由於頁面已刷新,模式已關閉。我希望模態在提交表單時保持開放。下面的PHP是下式給出:如何使用PHP驗證引導模式中的表單?

<?php 
$nameerror = $studentiderror = $emailerror = ""; 

$subject = "Thank you for voting!"; 
$body = "Dear $_POST["name"], Thank you for voting for this sustainable initiative. Your vote is appreciated! Lots of love from the Beefarm Committee of the Erasmus Sustainability Hub!"; 

if ($_SERVER["REQUEST_METHOD"] == "POST") { 

    if ($_POST["submit"]) { 

     if (empty(($_POST["name"])) { 
      $nameerror = "Please enter your name" } 


     if (empty($_POST["studentID"])) { 
      $studentiderror .= "Please enter your student ID"; 

     } else { 
      $result = "Thank you for voting!"; 
      mail($_POST["email"], $subject, $body); 
     } 

    }; 
} 

這裏是模態的HTML,PHP的嵌入在HTML,其中當表單提交後驗證錯誤消息應顯示:

<div class="container"> 
    <div class="modal" id="myModal"> 
     <div class="modal-dialog"> 
      <div class="modal-content"> 
       <div class="modal-header"> 
        <button class="close" data-dismiss="modal">x</button> 
        <h4 class="modal-title">Vote!</h4> 
       </div> 

       <div class="modal-body"> 
        <p> 
         Let us know what you think! <br/> 
         By voting, you as a student can have a say in the realisation of this project. Even if you vote 
         against having bees on campus, your vote is appreciated! <br/> 
         Thank you! 
        </p> 
        <form id="votingscreen" method="POST"> 
         <?php echo $result ?> 
         <div class="form-group"> 
          <label for="name">Name</label> 
          <?php echo $nameerror ?> 
          <input style="width: 180px" type="text" id="name" name="name" class="form-control" placeholder="Your name"> 
         </div> 
         <div class="form-group"> 
          <label for="studentID">Student ID</label> 
          <?php echo $studentiderror ?> 
          <input style="width: 180px" type="text" id="studentID" name="studentID" class="form-control" placeholder="123456AB"> 
         </div> 
         <div class="form-group"> 
          <label for="email">E-mail</label> 
          <?php echo $emailerror ?> 
          <input style="width: 180px" type="email" id="email" name="email" class="form-control" placeholder="E-mail"> 
         </div> 
         <hr /> 
         <div class="radiogroup"> 
          <label for="radiogroup"> Bees on campus? </label> 
          <label class="radio-inline"> 
           <input type="radio" name="vote" id="voteyesradio" value="yes" checked> 
           Yes 
          </label> 
          <label class="radio-inline"> 
           <input type="radio" name="vote" id="votenoradio" value="no"> 
           No 
          </label> 
         </div> 
         <div class="form-group"> 
          <input type="submit" name="submit" id="submitButton" class="btn btn-success btn-block" value="Vote!"/> 
         </div> 
        </form> 
       </div> 
      </div> 
     </div> 
    </div> 
</div> 

這是JS代碼:

$(".contentContainer").css("min-height", $(window).height()); 
$("#image").css("min-height", $("#beeweek").height()); 
$(document).ready(function() { 

    $("#submitButton").click(function() { 

     $("#myModal").modal('show'); 
    }) 
}); 
$(".video-container").css("min-height", $(window).height()); 
+0

昨天我回答了一個非常類似的問題,並添加了一個示例:http://stackoverflow.com/questions/42602864/php-show-alert-before-excuting-the-code/42602972#42602972 – Yolo

+0

Ajax是因爲您需要提交表單並獲得響應而無需刷新頁面。 – neophyte

回答