doumei1772 2015-12-08 04:12
浏览 39
已采纳

Bootstrap Modal PHP提交

I am working on a site, and having never done something like this, I am unsure how to proceed with PHP. I am working with bootstrap and looking to send an email based on a modal. My HTML looks like

   <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>

So I am just wondering if anyone could give me some pointers to make this form actually send the email. I am looking to learn and willing to try anything. Thanks in advance, help and constructful criticism appreciated. Also, how would I approach adding a confirmation that the message has been sent?

Thanks, John.

<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>

This is the script I tried to add to call the 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:
 Name: $name 
 ".
"Email: $email
 Message 
 $message";
$headers = "From: $myemail
";
$headers .= "Reply-To: $email";
mail($to,$email_subject,$email_body,$headers);
}
?>

And I mean I am sure there is something I am missing, or some simple fix, this is all just so new to me.

  • 写回答

2条回答 默认 最新

  • doujiao1948 2015-12-08 05:18
    关注

    Following are the edits for you:

    1) You have two forms in the modal and none of them having closing form tag. Remove <form class="form-horizontal"> form and change your other form to <form class="contact">

    2) Give closing form tag.

    3) Change submit button to <button class="btn btn-success" id="submit"><i class="glyphicon glyphicon-inbox"></i> Submit</button>

    4) Also check updated AJAX code and PHP code.

    Hope this should help you..

    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:
     Name: $name 
     ".
            "Email: $email
     Message 
     $message";
            $headers = "From: $myemail
    ";
            $headers .= "Reply-To: $email";
    
            if(mail($to,$email_subject,$email_body,$headers))
                echo $msg_success;
            else
                echo $msg_fail;
        }
        else
            echo "Input parameters missing";
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?