heres my ajax:
$('#savecustomer').click(function() {
$.ajax({
type: "POST",
url: "ajax-newcustomer.php",
data: ({ name : $('#ncname').val(), address : $('#ncaddress').val(), postcode : $('#ncpostcode').val(), dob : $('#ncdob').val(), mobile : $('#ncmobile').val(), email : $('#ncemail').val() }),
dataType: "json",
success: function(data){
alert('sc success');
if (data == 'error')
{
$('#ncsuccess').attr('display', 'none');
$('#ncerror').attr('display', 'block');
}
else
{
$('#ncname').val('Name');
$('#ncaddress').val('Address');
$('#ncpostcode').val('Postcode');
$('#ncdob').val('Date of Birth');
$('#ncmobile').val('Mobile Number');
$('#ncemail').val('Email Address');
$('#ncerror').attr('display', 'none');
$('#ncsuccess').html(data);
$('#ncsuccess').attr('display', 'block');
}
}
});
});
its not showing me the alert at all. meaning it isn't succeeding....i'm guessing its something to do with the data: format.
the data should be an object literal in your case without the ()
...
data: {
name : $('#ncname').val(),
address : $('#ncaddress').val(),
postcode : $('#ncpostcode').val(),
dob : $('#ncdob').val(),
mobile : $('#ncmobile').val(),
email : $('#ncemail').val()
},
...
in addition - i would gather all the values before the ajax call in the following way:
var ncname = $('#ncname').val();
this will let you verify the valuse in a more convenient way in firebug/chrome dev tools oh.. almost forgot - check fiddler or other tool to see if a request was even sent to the server to see if it rejected it
Remove the brackets ( and ) around the data.
I suggest you to use the error
callback of the ajax
method to help you see what is happening:
error: function(xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
This way you can see if it's a 404 (so the page path is wrong), a 500 (some server-side error) etc.
add error handler and see what error is returned:
$.ajax({
type: "POST",
url: "ajax-newcustomer.php",
data: (...),
dataType: "json",
success: function(data){
...
},
error: function(err){ ... check err here ...}
});
Also, no brackets around data