jQuery ajax error function called even on successful request

Viewed 9488

I'm working on my wordpress site contact form that I want to be sent without page refreshing. I've managed to get it working, it sends the mail, but then I thought that what if it doesn't succeed, and added the error function. Now, it calls the error function every time, even if it manages to send the mail. Here's the code for it:

    $("#Submit").click(function () {
    var dataString = $('#knamecontactform').serialize();
    //alert (dataString);return false;
    $.ajax({
        type: "POST",
        url: "http://example.com/form.php",
        data: dataString,
        dataType: "text",
        error: function () {
            $('#messagenotsent').fadeIn(400);
        },
        success: function () {
            $('#knamecontactform')[0].reset();
            $('#messagesent').fadeIn(400);
        }


    });
    return false;

});

And the submitform.php:

<?php
$to = "mail@gmail.com";
$subject = "Message";
$message = 
$_POST['Nimi'] . "\n" .
$_POST['Puhelin'] . "\n" .
$_POST['Sposti'] . "\n" .
$_POST['Tiedot'];
$from = $_POST['Nimi'];
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo 'Mail sent.';
?>
1 Answers
Related