Yisroel Arnson Yisroel Arnson - 4 months ago 12
PHP Question

.done ajax giving me an error

So I'm creating a contact form that sends an email using ajax and PHP.

I Seem to get an error when i run this script.


error: Uncaught Syntax Error: Unexpected token .


using chrome BTW.

I also have a PHP file, but i don't think that is relevant, i can add it if needed.

$(function() {
var form = $('#ajax-contact');
var formMessages = $('#form-messages');
var formData = $(form).serialize();


$(form).submit(function(event) {
event.preventDefault();
});

$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: formData
});

.done(function(response) {
$(formMessages).removeClass('error');
$(formMessages).addClass('success');

$(formMessages).text(response);

$('#name').val('');
$('#email').val('');
$('#message').val('');
})

.fail(function(data) {
$(formMessages).removeClass('success');
$(formMessages).addClass('error');

if (data.responseText !== '') {
$(formMessages).text(data.responseText);
} else {
$(formMessages).text('Oops! An error occured and your message could not be sent.');
}
});
});

Answer

This is happening because you are terminating the ajax call by putting a semicolon and after that you are calling .done(). The correct syntax is:

$.ajax( "..." )
.done(function() 
{ 
    alert("success"); 
});
Comments