使用ajax提交表单后获取警报消息

我已经编写了通过 ajax 发送电子邮件的代码。单击按钮后,电子邮件已成功发送,但我没有收到警报消息,这是为什么,我的代码中有什么问题,请帮助我找出错误。


这是我的 Ajax 代码


<script>


    $(function () {

        $("#myForm").submit(function (evenr) {

            event.preventDefault(e);

            var form_data = $(this).serialize();

            $.ajax({

                url:'mail.php',

                method:'POST',

                data:{data},

                success:function(data){

                    alert(data);

                    $("#success_message").attr("style", "display: none");

                }

            });

        });

    });


</script>

这是我的 html 表单


<div class="col-lg-5 col-md-6 home-about-left">

    <form class="form-area contact-form text-right" id="myForm">

     <h4 style="text-align: left;color: #ffffff;font-size: 24px;">Get a free consultation</h4></br>

     <div class="row">


         <div class="col-lg-12 form-group">

             <div class="alert alert-success" id="success" style="display: none;">

                 <strong>Success!</strong> Indicates a successful or positive action.

             </div>

         </div>


      <div class="col-lg-12 form-group">

       <input name="name" placeholder="Name" id="formClass" class="common-input mb-20 form-control" required="" type="text">

       <input name="phone" placeholder="Phone Number" id="formClass" class="common-input mb-20 form-control" required="" type="text">

       <input name="email" placeholder="Email" id="formClass" pattern="[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{1,63}$" class="common-input mb-20 form-control" required="" type="email">

       <input name="service" placeholder="Type of Pest control services" id="formClass" class="common-input mb-20 form-control" required="" type="text">

      </div>

      <div class="col-lg-12">

       <!--<button class="genric-btn primary" style="float: left;">Send Message</button>-->

      <input name="submit" type="submit" class="genric-btn primary" style="float: left;" value="Send Message">

      </div>

     </div>

    </form>

   </div>


幕布斯7119047
浏览 210回答 2
2回答

SMILET

<script>&nbsp; &nbsp; $(function () {&nbsp; &nbsp; &nbsp; &nbsp; $("#myForm").submit(function (evenr) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.preventDefault(e);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var form_data = $(this).serialize();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url:'mail.php',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method:'POST',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data:{data},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success:function(data){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($.trim(data))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alert("Mail was sent !")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#success_message").attr("style", "display: none");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}else{ alert("Mail was not sent !")}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });</script>Ajax 文件更改:if(isset($_POST['name']) && isset($_POST['phone']) && isset($_POST['email']) && isset($_POST['service'])) {&nbsp; &nbsp; &nbsp; &nbsp; $to = 'test@gmail.com';&nbsp; &nbsp; &nbsp; &nbsp; $subject = "{$_POST['name']} " . ' Requested the Pest Control Services';&nbsp; &nbsp; &nbsp; &nbsp; $message = '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <html>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <title>Pest Control Services</title>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </head>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><b>Name:</b> ' . $_POST['name'] . '</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><b>Phone:</b> ' . $_POST['phone'] . '</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><b>Email:</b> ' . $_POST['email'] . '</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><b>Services Type:</b> ' . $_POST['service'] . '</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </body>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </html>';&nbsp; &nbsp; &nbsp; &nbsp; $headers = "Content-type: text/html; charset=utf-8 \r\n";&nbsp; &nbsp; &nbsp; &nbsp; $headers .= "From:{$_POST['name']} {$_POST['email']}\r\n";&nbsp; &nbsp; &nbsp; &nbsp; if(mail($to, $subject, $message, $headers)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "1"; exit;&nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "0"; exit;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

沧海一幻觉

尝试这样的事情..if((isset($_POST['name']))&&(isset($_POST['phone'])&&$_POST['email']&&$_POST['service'])) {&nbsp; &nbsp; &nbsp; &nbsp; $to = 'ajerad30@gmail.com';&nbsp; &nbsp; &nbsp; &nbsp; $subject = "{$_POST['name']} " . ' Requested the Pest Control Services';&nbsp; &nbsp; &nbsp; &nbsp; $message = '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <html>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <title>Pest Control Services</title>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </head>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><b>Name:</b> ' . $_POST['name'] . '</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><b>Phone:</b> ' . $_POST['phone'] . '</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><b>Email:</b> ' . $_POST['email'] . '</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><b>Services Type:</b> ' . $_POST['service'] . '</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </body>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </html>';&nbsp; &nbsp; &nbsp; &nbsp; $headers = "Content-type: text/html; charset=utf-8 \r\n";&nbsp; &nbsp; &nbsp; &nbsp; $headers .= "From:{$_POST['name']} {$_POST['email']}\r\n";&nbsp; &nbsp; &nbsp; &nbsp;if(mail($to, $subject, $message, $headers))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<script>alert('Mail was sent !');</script>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<script>alert('Mail was not sent. Please try again later');</script>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP