0

In a php script, after an attempt at a database insert, I'm returning to the calling $.ajax() function either the id of the new record or an error message:

if ($newID > 0) {
    // Successfully added new record
    echo $newID;
} else {
    // No new record was inserted
    echo 'Error: ' . mysqli_error($dbc);
}

My question is: if the php else clause gets fired and I'm passing a string via the echo, then won't the success() function be called in the $.ajax function? Is there a way to somehow "fire" the $.ajax error() function depending on what's passed from the php script?

1 Answer 1

1

$.ajax.error() shouldn't really be fired, as the AJAX call was a success. However, I guess you could return a different header if you really wanted to:

header("HTTP/1.0 404 Not Found");

See http://php.net/manual/en/function.header.php and http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

Sign up to request clarification or add additional context in comments.

2 Comments

I kind of thought so. If I'm getting the new ID in the php script with: "$newID = @mysqli_insert_id($dbc);" and the insert didn't work, then would the error function in the ajax call fire anyway? (I.e. do I really need the if/else code in my question?)
If the insert fails (and fails silently), the page should still return a 200, so you'll need your if/else to detect if you should return your custom header or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.