how to use the error parameter of the jquery’s $.ajax thing ? let’s say i have a form, and then when I submit it, the data is being saved to the table using the PHP script used in the ,
$.ajax({
type: "POST",
url: "classes/ajax.submitcv.php",
data: "userid="+userid+"&resumetitle="+resumetitle+"&resumeintro="+resumeintro+
"&name="+name+"&dob="+dob+"&contacttel1="+contacttel1+"&contacttel1type="+contacttel1type+
"&contacttel2="+contacttel2+"&contacttel2type="+contacttel2type+"&contacttel3="+contacttel3+
"&contacttel3type="+contacttel3type+"&primaryemail="+primaryemail+"&secondaryemail="+secondaryemail+
"&skype="+skype+"&facebook="+facebook+"&linkedin="+linkedin+"&twitter="+twitter+
"&messenger="+messenger+"&yahoo="+yahoo+"&aol="+aol+"&summaryofpositionsought="+
summaryofpositionsought+"&summaryofskills="+summaryofskills+"&gender="+gender,
success: function(){
$('form#wsrecruitcvhead').fadeOut("normal",function(){
$('div.success').fadeIn(1000);
});
},
});
return false;
});
});
based from the code above, if the data went through the database table, the form disappears and the success message inside a tag shows, so how am I gonna output an error message instead, let’s say I have a php function and the data to be inserted returns false, how to update the html file given the message and the form won’t fadeOut ? in short..how to use the error param of $.ajax ?
The error() function is used to call a function whenever the request fails. We’re going to assume that your request doesn’t fail (in an HTTP sense; that is, the request can return with an error without failing). What you need to do is have ajax.submitcv.php print out some sort of text indicating whether the data was successfully saved. Then, in your success() function, you can do something like:
I would recommend sending the results back as JSON. Something like:
And:
By the way, I’m being very general. Actual production code should check for null values and other things (and have an error handler for when requests fail).