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/

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

Answers


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.ajax({
           url: '/contact.php',
           data: postData
       }).done(function (html) {
           alert(html);
       });
       event.preventDefault();
   }; // <-- HERE

And your HTML is malformed:

<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.ajax({
      type: "POST", // THIS
      url: '/contact.php',
      data: postData
      ...

Need Your Help

EJB 3.0. Provide interfaces to clients

java java-ee ejb-3.0

Suppose I am writing enterprise session bean(v 3.0) for using it as remote bean.

Group not working in regex

c# regex

I wrote a regex for finding id values of html elements:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.