简体   繁体   English

无法使用Ajax在联系表单中发送电子邮件

[英]Failure to send an email in a contact form using Ajax

I am very close to finishing this little project and all I need to do is make my contact form send emails. 我非常接近完成这个小项目,我所要做的就是使我的联系表发送电子邮件。 Easy Right? 容易吧? No. lol 不好笑

I have got the contact form to close after a message is submitted but does not send the email and was wondering if anyone could help me with this? 提交消息后,我已经关闭了联系表单,但是没有发送电子邮件,并且想知道是否有人可以帮助我? My code is below: 我的代码如下:

http://madaxedesign.co.uk/dev/contact.php http://madaxedesign.co.uk/dev/ http://madaxedesign.co.uk/dev/contact.php http://madaxedesign.co.uk/dev/

Jquery: var doSubmit = function (event) { var postData = jQuery('#submit_message').serialize(); jQuery:var doSubmit = function(event){var postData = jQuery('#submit_message')。serialize();

      jQuery.ajax({
        type: "POST",
        url: '/dev/contact.php',
        data: postData
      }).done(function( html ) {
        alert(html);
      });
      event.preventDefault();
    };

        $(function () {
        $('#submit_message').submit(doSubmit);
    });

PHP: PHP:

        <?php
        $your_email = "maxlynn@madaxedesign.co.uk";
        $subject = "Email From Madaxe";
        $empty_fields_message = "<p>Please go back and complete all the fields in the form.</p>";
        $thankyou_message = "<p>Thank you. Your message has been sent. We Will reply as soon as possible.</p>";

        $name = stripslashes($_POST['txtName']);
        $email = stripslashes($_POST['txtEmail']);
        $message = stripslashes($_POST['txtMessage']);

        if (!isset($_POST['txtName'])) {

        ?>

        <form id="submit_message" class="hide_900">
            <div id="NameEmail"> 
                <div>
                    <label for="txtName">Name*</label> 
                    <input type="text" title="Enter your name" name="txtName" />
                </div> 
                <div>
                    <label for="txtEmail">Email*</label> 
                    <input  type="text" title="Enter your email address" name="txtEmail" />
                </div> 
            </div>
            <div id="MessageSubmit">
                <div> 
                    <textarea maxlength="1200" title="Enter your message" name="txtMessage"></textarea> 
                    <label for="txtMessage">Message</label>
                </div>
                <div class="submit"> 
                    <input type="submit" value="Submit" /></label>
                </div>
            </div> 
        </form> 
        <?php

        }

        elseif (empty($name) || empty($email) || empty($message)) {

        echo $empty_fields_message;

        }

        else {

            $referer = $_SERVER['HTTP_REFERER'];
            $this_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];
        if ($referer != $this_url) {
        echo "You do not have permission to use this script from another URL, nice hacking attempt ;p.";
        exit;
            }

        mail($your_email, $subject, $message, "From: $name <$email>");

        echo $thankyou_message;

            }

        ?>

Thanks for your help 谢谢你的帮助

You should debug your code to see if there is any error or bug and then include the error in your question to get a better help. 您应该调试代码以查看是否有任何错误或错误,然后将错误包括在问题中以获得更好的帮助。 But anyway, by a quick look you've got one semicolon missing in your jQuery: 但是无论如何,通过快速浏览,您在jQuery中缺少了一个分号:

       jQuery.ajax({
           url: '/contact.php',
           data: postData
       }).done(function (html) {
           alert(html);
       });
       event.preventDefault();
   }; // <-- HERE

And your HTML is malformed: 而且您的HTML格式不正确:

<div class="submit"> 
      <input type="submit" value="Submit" /></label> <!-- HERE the `label` -->
</div>

Check these two and report back. 检查这两个并报告。 I'm afraid that it won't fix your problem but it's worth trying. 恐怕它不能解决您的问题,但是值得尝试。

Update: 更新:

Add this to your jQuery: 将此添加到您的jQuery:

jQuery.ajax({
      type: "POST", // THIS
      url: '/contact.php',
      data: postData
      ...

Used new PHP and took out my own Jquery and now have to figure out a way to redirect a thank you message in my pop up. 使用新的PHP并取出我自己的Jquery,现在必须找出一种方法来重定向弹出窗口中的感谢消息。 Thanks for your help Sam 谢谢您的帮助,山姆

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

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