6

I have PHP code snippet the following:

if (!($result = mysql_query($query, $link))) {
   die("Invalid SQL query: " . $query);
}

And I have JQuery code snippet the following:

$.ajax({
        url: "....search.php", 
        data: ...,

        async: false, //to trigger error alert

        success: function(xml) {
            ...
        },

        error: function(xml) {
            foundError = true;
        },

        dataType: "xml"
    });

if(foundError) {
        setProgress("Could not complete the search because an error was found", ProgressBar.ERROR);
}

Is it possible to have the die call trigger JQuery error function callback? If not, how would I trigger it otherwise?

3 Answers 3

9

Try this:

if (!($result = mysql_query($query, $link))) {
   header("HTTP/1.1 404 Not Found");
}

Choose the error appropriate for your application

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

4 Comments

Cool! If I send data before this (ex. before PHP code posted I had echo "hello world";) then this would not work, correct? Wondering if there are other ways.
@BlackSheep, header() calls must be made before other output (i.e. echo "hello world").
You could easily call ob_start() at the top of the page, ob_end_flush() at the bottom, and then call ob_clean() right before your header to erase the output buffer if anything is in it.
Thanks very much. I did not know about the ob_X methods. They are very useful. =)
0

You cannot signal client-side code from the server (at least, not in the way you're expecting).

When using AJAX, best to use proper HTTP response codes and have jQuery act upon those in the error: callback.

Comments

0

You cannot access the ajax error like that as there was no ajax error. What you could do, is check your xml variable in the success section; if it starts with Invalid SQL query you know there was a php error.

To tidy things up, you probably want to return some more information (xml / json) in case of an error instead of just a string.

Comments

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.