Bootstrap Modal PHP提交

时间:2015-12-08 04:12:41

标签: php html5 modal-dialog bootstrap-modal

我正在网站上工作,从未做过这样的事情,我不确定如何继续使用PHP。我正在使用bootstrap并希望发送基于模态的电子邮件。我的HTML看起来像

   <div class="modal fade" id="contact" role="dialoge">
        <div class="modal-dialog">
            <div class="modal-content">
                <form class="form-horizontal">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>

                        <h4>Plan your Party!</h4>

                    </div>

                    <form class="name" name="contact">
                        <div class="modal-body">

                            <div class="form-group">

                                <label for="name" class="col-lg-2 control-label">Name:</label>

                                <div class="col-lg-10">
                                    <input type="text" class="form-control" name="name" required="required" placeholder="Full Name">
                                </div>l

                            </div>

                            <div class="form-group">

                                <label for="email" class="col-lg-2 control-label">Email:</label>

                                <div class="col-lg-10">
                                    <input type="email" class="form-control" name="email" required="required" placeholder="Email">
                                </div>

                            </div>

                            <div class="form-group">

                                <label for="message" class="col-lg-2 control-label">Message:</label>

                                <div class="col-lg-10">
                                    <textarea class="form-control" rows="8" style="resize: vertical;" required="required" placeholder="Message" name="message"></textarea>
                                </div>

                            </div>
                        </div>
                        <div class="modal-footer">

                            <button type="button" class="btn btn-info" data-dismiss="modal"><i class="glyphicon glyphicon-remove"></i> Close</button>
                            <button class="btn btn-success" type="submit" id="submit"><i class="glyphicon glyphicon-inbox"></i> Submit</button>

                        </div>
            </div>
        </div>
    </div>

所以我只是想知道是否有人可以给我一些指示,让这个表格实际发送电子邮件。我希望学习并愿意尝试任何事情。在此先感谢,帮助和建设性的批评赞赏。另外,如何添加确认消息已发送?

谢谢,约翰。

<script>
    $(document).ready(function () {
        $("input#submit").click(function(){
            $.ajax({
                type: "POST",
                url: "process.php", // 
                data: $('form.contact').serialize(),
                success: function(msg){
                    $("#thanks").html(msg)
                    $("#form-content").modal('hide');   
                },
                error: function(){
                    alert("failure");
                }
            });
        });
    });
</script>

这是我试图添加的脚本来调用php:

<?php

$myemail = 'myemail@myemail.ca';


if (isset($_POST['name'])) {
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$message = strip_tags($_POST['message']);
echo "<span class=\"alert alert-success\" >Your message has been received. Thanks! Here is what you submitted:</span><br><br>";
echo "<stong>Name:</strong> ".$name."<br>";   
echo "<stong>Email:</strong> ".$email."<br>"; 
echo "<stong>Message:</strong> ".$message."<br>";


$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email";
mail($to,$email_subject,$email_body,$headers);
}
?>

我的意思是我确信有一些我缺少的东西,或者一些简单的修复,这对我来说都是如此新鲜。

2 个答案:

答案 0 :(得分:0)

form标记中包含action属性以指向任何PHP页面,您可以从那里发送电子邮件...

答案 1 :(得分:0)

以下是您的修改:

1)你在模态中有两个表单,而且没有一个表单具有结束表单标签。删除<form class="form-horizontal">表单并将其他表单更改为<form class="contact">

2)给出结束表格标签。

3)将提交按钮更改为<button class="btn btn-success" id="submit"><i class="glyphicon glyphicon-inbox"></i> Submit</button>

4)同时检查更新的AJAX代码和PHP代码。

希望这可以帮助你..

<强> HTML

<div id="thanks"> </div>
<p>
    <a data-toggle="modal" href="#contact" 
    class="btn btn-primary">Contact us</a>
</p>
<!-- model content --> 
<div class="modal hide fade in" style="display: none; " id="contact" > 
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4>Plan your Party!</h4>
            </div>
            <form class="contact">
                <div class="modal-body">
                    <div class="form-group">
                        <label for="name" class="col-lg-2 control-label">Name:</label>
                        <div class="col-lg-10">
                            <input type="text" class="form-control" name="name" required="required" placeholder="Full Name">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="email" class="col-lg-2 control-label">Email:</label>
                        <div class="col-lg-10">
                            <input type="email" class="form-control" name="email" required="required" placeholder="Email">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="message" class="col-lg-2 control-label">Message:</label>
                        <div class="col-lg-10">
                            <textarea class="form-control" rows="8" style="resize: vertical;" required="required" placeholder="Message" name="message"></textarea>
                        </div>
                    </div>
                </div>
            </form> 
            <div class="modal-footer">
                <button type="button" class="btn btn-info" data-dismiss="modal"><i class="glyphicon glyphicon-remove"></i> Close</button>
                <button class="btn btn-success" id="submit"><i class="glyphicon glyphicon-inbox"></i> Submit</button>
            </div>
        </div>
    </div>
</div>

<强> AJAX

<script>
 $(function() {
    //twitter bootstrap script
    $("button#submit").click(function(){
        $.ajax({
                type: "POST",
                url: "process.php",
                data: $('form.contact').serialize(),
                success: function(msg){
                    $("#thanks").html(msg);
                    $("#contact").modal('hide'); 
                },
                error: function(){
                    alert("failure");
                }
       });
    });
});
</script>

<强> process.php

<?php
    $myemail = 'myemail@myemail.ca';
    if (isset($_POST['name'])) 
    {
        $name = strip_tags($_POST['name']);
        $email = strip_tags($_POST['email']);
        $message = strip_tags($_POST['message']);

        $msg_success = "";
        $msg_fail = "Error sending mail.";
        $msg_success .= "<span class=\"alert alert-success\" >Your message has been received. Thanks! Here is what you submitted:</span><br><br>";
        $msg_success .= "<stong>Name:</strong> ".$name."<br>";   
        $msg_success .= "<stong>Email:</strong> ".$email."<br>"; 
        $msg_success .= "<stong>Message:</strong> ".$message."<br>";

        $to = $myemail;
        $email_subject = "Contact form submission: $name";
        $email_body = "You have received a new message. ".
        " Here are the details:\n Name: $name \n ".
        "Email: $email\n Message \n $message";
        $headers = "From: $myemail\n";
        $headers .= "Reply-To: $email";

        if(mail($to,$email_subject,$email_body,$headers))
            echo $msg_success;
        else
            echo $msg_fail;
    }
    else
        echo "Input parameters missing";
?>