[英]How to open a bootstrap modal with PHP?
我正在創建一個包含用於RSVP的PHP表單的婚禮網站。 我充其量是新手。
我正在嘗試做的是,一旦用戶填寫並提交了表單,網頁要么將他們帶到成功/謝謝頁面。 (這部分正在工作) 。
或者,如果未正確填寫表格,則會顯示一個引導程序模式,告訴他們他們做錯了什么。 (這就是我遇到的麻煩) 。
這是我的與模態部分有關的代碼。
我認為,當出現任何類型的錯誤時,我的PHP if
語句應運行JavaScript。 並且JavaScript應該打開一個顯示錯誤的模式窗口。 什么不連接?
<?php if ($_POST["submit"]) { $result='<div class="alert alert-success">Form submitted</div>'; if (!$_POST["name"]) { $error.="<br> Please enter the name on your invitation."; } if (!$_POST["head-count"]) { $error.="<br> Please enter the size of your party."; } if (!$_POST["reception-check"]) { $error.="<br> Please let us know if you will be attending the reception."; } if ($error) { ?> <script type="text/javascript"> $('#myModal').modal('show'); </script> <?php } else { if (mail("dprb17@gmail.com", "RSVP", " Name: ".$_POST['name']." Head Count: ".$_POST['head-count']." Reception Check: ".$_POST['reception-check']." Comments: ".$_POST['comments'])) { header("location: http://www.ourpeachwedding.com/pages/thankyou.php"); exit(); } else { $result='<div class="alert alert-danger"><strong>Sorry, there was an error submitting your rsvp, please try again.</strong>'.$error.'</div>'; } } } ?> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h4 class="modal-title" id="myModalLabel">Modal title</h4> </div> <div class="modal-body"> <?php echo $result ?> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div> </div>
<div id="rsvp"> <div class="jumbotron"> <?php echo $result; ?> <div class="container-fluid"> <p class="text-center h1">RSVP</p> <p class="lead text-center">Even if you are not planning to attend, please RSVP anyway.</p> <div class="row"> <div class=" col-md-6 col-md-offset-3"> <form method="post"> <div class="input-group"> <span class="input-group-addon">Name:</span> <input name="name" type="text" class="form-control" placeholder="What was the name on the invitation?" value="<?php echo $_POST['name']; ?>"> </div> <!--/.input-group--> <hr> <div class="input-group"> <span class="input-group-addon">Head Count:</span> <input name="head-count" type="text" class="form-control" placeholder="How many in your party?" value="<?php echo $_POST['head-count']; ?>"> </div> <!--/.input-group--> <hr> <div class="input-group"> <span class="input-group-addon">Reception?</span> <input name="reception-check" type="text" class="form-control" placeholder="Will you be attending the reception?" value="<?php echo $_POST['reception-check']; ?>"> </div> <!--/.input-group--> <hr> <div class="input-group"> <span class="input-group-addon">Comments:</span> <textarea name="comments" type="text" class="form-control" placeholder="eg. gluten/food allergies, not attending, etc."><?php echo $_POST['comments']; ?></textarea> </div> <!--/.input-group--> <hr> <div class="text-center"> <input type="submit" name="submit" class="btn btn-success btn-large" value="Submit"> </div> <!--/.text-center--> </form> </div> <!--/.col--> </div> <!--/.row--> </div> <!--/.container-fluid--> </div> <!--/.jumbotron--> </div> <!--/#rsvp-->
我認為出於這個目的,是否在客戶端進行驗證不是很重要。 您可以根據需要將其更改為客戶端驗證,但是要添加的腳本無法運行,因為它在加載必要的html元素之前就已在運行。 為了確保它能繼續運行,請將該行更改為
if ($error) { ?>
<script type="text/javascript">
$(document).ready(function() {
$('#myModal').modal('show');
});
</script>
<?php
} else {
...
該腳本僅在出現錯誤時才添加,因此它將在加載時運行,但僅在提交並發現錯誤后重新加載頁面時運行。
我遇到了同樣的問題,並解決了該問題:您必須echo
顯腳本,因為當結束php代碼?>
並重新啟動<?php
,結束和開始之間的代碼將被忽略或始終可見/啟用。 請參閱下面的示例,我認為這會起作用!
if ($error) { ?>
<script type="text/javascript"> $('#myModal').modal('show'); </script>
<?php
}
必須
if ($error) {
echo '<script type="text/javascript"> $("#myModal").modal("show")</script>';
}
在'
在腳本中替換"
,因為'
會忽略"
所以它只是它呼應
您代碼中的其他一些內容:在if ($error)
或if (!$_Post[...])
T$_Post[...] > "1"
無法正常工作,因為沒有諸如$error = 1
或T$_Post[...] > "1"
在這種情況下,if將永遠不會運行,在這里:
if (mail("dprb17@gmail.com", "RSVP", "
Name: ".$_POST['name']."
Head Count: ".$_POST['head-count']."
Reception Check: ".$_POST['reception-check']."
Comments: ".$_POST['comments'])) {
header("location: http://www.ourpeachwedding.com/pages/thankyou.php");
exit();
沒有任何后果
我認為這項工作會非常好! 我希望這會成功! 祝您好運...。並檢查您的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.