简体   繁体   English

php表单提交后打开引导模态

[英]open bootstrap modal after php form submit

I'm trying to submit my contact form, send the data in an email with PHP, stay on index.html and open a bootstrap modal with a "Thank you" message. 我正在尝试提交我的联系表,使用PHP通过电子邮件发送数据,保留在index.html上,并打开一条带有“谢谢”消息的引导程序模式。

All the other examples I've seen that might do this have used AJAX. 我看到的所有其他可能这样做的示例都使用了AJAX。 BUT I'd like to do it through PHP alone as my AJAX knowledge is non existent. 但是我想仅通过PHP来完成此操作,因为我的AJAX知识不存在。 Does anyone know if its even possible? 有谁知道它甚至可能吗? and if so, how? 如果是这样,怎么办?

I've only included the last part that makes sure I return to index.html or displays the errors from the form submission 我只包括确保我返回index.html或显示表单提交错误的最后一部分

PHP from contact.php. 来自contact.php的PHP。

if(!$mail->Send()){
    echo "Message could not be sent.";
    echo "Mailer Error: " . $mail->ErrorInfo;
    exit;
}
print '<meta http-equiv="refresh" content="0; url=index.html" />';
echo "<script>$('#myModal').modal('show')</script>";

HTML 的HTML

<form id='contactus' action='contact.php' enctype="multipart/form-data" method='post'>
<fieldset>
<legend>Contact us</legend>
<div class='container'>  
    <label for='email' >Name*:</label><br/>
    <input type="text" id="name" name="name" required /><br>
    <label for='email' >Phone*:</label><br/>
    <input type="text" id="phone" name="phone" required /><br>
    <label for='email' >Email*:</label><br/>
    <input type='text' name='email' id='email' required/><br/>
    <label for='message' >Message:</label><br/>
    <textarea rows="10" cols="50" name='message' id='message'></textarea>
        <!-- MAX_FILE_SIZE must precede the file input field -->
    <br>
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input id="file" name="image" type="file" />
    <input type='submit' name='Submit' value='Submit' />
</div>
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Thanks!</h4>
      </div>
      <div class="modal-body">
        <p>Thank you for your message!</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

here : So if I get it right, on click of a button, you want to open up a modal that lists the values entered by the users followed by submitting it. 此处 :因此,如果我做对了,请单击按钮,然后打开一个模式,该模式列出用户输入的值,然后提交。

For this, you first change your input type="submit" to input type="button" and add data-toggle="modal" data-target="#confirm-submit" so that the modal gets triggered when you click on it: 为此,您首先将输入type =“ submit”更改为input type =“ button”并添加data-toggle =“ modal” data-target =“#confirm-submit”,以便在单击模式时触发该模式:

<input type="button" name="btn" value="Submit" id="submitBtn" data-toggle="modal" data-target="#confirm-submit" class="btn btn-default" />

Next, the modal dialog: 接下来,模式对话框:

<div class="modal fade" id="confirm-submit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                Confirm Submit
            </div>
            <div class="modal-body">
                Are you sure you want to submit the following details?

                <!-- We display the details entered by the user here -->
                <table class="table">
                    <tr>
                        <th>Last Name</th>
                        <td id="lname"></td>
                    </tr>
                    <tr>
                        <th>First Name</th>
                        <td id="fname"></td>
                    </tr>
                </table>

            </div>

  <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
            <a href="#" id="submit" class="btn btn-success success">Submit</a>
        </div>
    </div>
</div>

Lastly, a little bit of jQuery: 最后,一点jQuery:

$('#submitBtn').click(function() {
     /* when the button in the form, display the entered values in the modal */
     $('#lname').html($('#lastname').val());
     $('#fname').html($('#firstname').val());
});

$('#submit').click(function(){
     /* when the submit button in the modal is clicked, submit the form */
    alert('submitting');
    $('#formfield').submit();
});

You haven't specified what the function validateForm() does, but based on this you should restrict your form from being submitted. 您尚未指定validateForm()函数的功能,但是基于此,您应该限制表单的提交。 Or you can run that function on the form's button #submitBtn click and then load the modal after the validations have been checked. 或者,您可以在表单的按钮#submitBtn单击上运行该功能,然后在检查验证后加载模态。 DEMO 演示

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

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