In my document I have this script:
$.ajax({ type:"POST",url:"ajax.php",data:data,
success: function() {
//onsuccess
},
error: function() {
//onerror
}
});
How can I, in the document ajax.php, deliberately throw an error? Is it sufficient to just throw an 400 HTTP Status Code or something? (And if so, is 400 the right one?)
Why I'm asking is that I want to use this to submit a form to the server, and if a field isn't filled out properly I want to alert the user through the error parameter in $.ajax. I know I could do this using javascript in the first place, but I'm hoping I could do it this way (so I don't have to write the verification two times). But of course by doing it this way, I can't separate the different fields from the other.
Based on what I want to use it for, can I somehow throw a "custom error", so if the field name hasn't been filled out properly, it returns the name of the field (i.e. name). Is it sufficient to do something like this:
In ajax.php:
if (isFilledOutProperly($name) == false) {
echo "name";
}
else {
echo "success";
}
And:
success: function(data) {
if (data == 'name') {
//error
}
else {
//success
}
},
I may be on the wrong track here, but if someone have a better suggestion on how I can do this, please tell me. Thanks!