简体   繁体   English

如何使用PHP打开引导模态?

[英]How to open a bootstrap modal with PHP?

I am creating a wedding website containing a PHP form for RSVPs. 我正在创建一个包含用于RSVP的PHP表单的婚礼网站。 I am a novice at best. 我充其量是新手。

What I am trying to do is, once a user fills out and submits the form, the web page either takes them to a success/thank-you page. 我正在尝试做的是,一旦用户填写并提交了表单,网页要么将他们带到成功/谢谢页面。 (this part is working) . (这部分正在工作)

Or if the form is not filled out correctly, it shows a bootstrap modal which tells them what they did wrong. 或者,如果未正确填写表格,则会显示一个引导程序模式,告诉他们他们做错了什么。 (this is what I am having trouble with) . (这就是我遇到的麻烦)

Here is my code that has to do with the modal part. 这是我的与模态部分有关的代码。

In my mind, my PHP if statement should run the JavaScript when there is any sort of error. 我认为,当出现任何类型的错误时,我的PHP if语句应运行JavaScript。 And the JavaScript should open a modal window showing the errors. 并且JavaScript应该打开一个显示错误的模式窗口。 What is not connecting? 什么不连接?

 <?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">&times;</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--> 

I think that for this purpose it's not very important whether validation is done client side or not. 我认为出于这个目的,是否在客户端进行验证不是很重要。 You can change it to client side validation if you wish, but the script you are adding isn't working because it's running before the necessary html elements are loaded. 您可以根据需要将其更改为客户端验证,但是要添加的脚本无法运行,因为它在加载必要的html元素之前就已在运行。 To ensure it runs after, change that line to 为了确保它能继续运行,请将该行更改为

if ($error) { ?>
    <script type="text/javascript">
        $(document).ready(function() {
            $('#myModal').modal('show');
        });
    </script>
    <?php
} else {
...

The script is added only when there is an error, so it will run when loaded but only when the page is reloaded after submission and errors were found. 该脚本仅在出现错误时才添加,因此它将在加载时运行,但仅在提交并发现错误后重新加载页面时运行。

I had the same problem and solved it: You had to echo the script because when you end the php code ?> and start it again <?php the code between the end and start will be ignored or always visable/enable. 我遇到了同样的问题,并解决了该问题:您必须echo显脚本,因为当结束php代码?>并重新启动<?php ,结束和开始之间的代码将被忽略或始终可见/启用。 See the exemple below i think that will work! 请参阅下面的示例,我认为这会起作用!

 if ($error) { ?>
    <script type="text/javascript"> $('#myModal').modal('show'); </script>
    <?php
 }

Has to be 必须

 if ($error) { 
   echo '<script type="text/javascript"> $("#myModal").modal("show")</script>';
 }

The ' in the script are replaced with " because a ' will ignore " so it just echo it '在脚本中替换" ,因为'会忽略"所以它只是它呼应

Some other things in your code: At the if ($error) or if (!$_Post[...]) It wont work because there is no requirement like $error = 1 or T$_Post[...] > "1" in this case the if will never run and here : 您代码中的其他一些内容:在if ($error)if (!$_Post[...]) T$_Post[...] > "1"无法正常工作,因为没有诸如$error = 1T$_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();

There is no consequence 没有任何后果

I think when this work it will be very nice! 我认为这项工作会非常好! I hope this will work! 我希望这会成功! Good Luck.... and check your code. 祝您好运...。并检查您的代码。

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

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