Daniel Mabinko
Daniel Mabinko

Reputation: 1401

Difficulty displaying error message using AJAX jQuery API and PHP.

I'm using the jQuery AJAX function in order to retrieve data from my mySQL database without having to refresh the page. I have everything in working order, my query's are retrieving the correct data from my database. However, I am struggling to echo out an error message when no data can be retrieved based on the users input. I have a php file that provides the user interface for the user to search from, it also contains the following Scripts in the document head.

Here is what I have so far:-

<script type="text/javascript">
$(function(){
    $('#users').keyup(function(){
        var inpvalue= $('#users').val();
    });
});
</script>



<script type="text/javascript">
$(function(){
            $('#users').keyup(function(){

            var inpval=$('#users').val();

            $.ajax({
                type: 'POST',
                data: ({p : inpval}),
                url: 'data.php',
                success: function(data) {
                     $('#output_div').html(data);

          }
        });
    });
});

</script>

Any help would be greatly appreciated, sorry if I haven't explained myself very well.
Thankyou.

Upvotes: 0

Views: 1200

Answers (3)

nathanjosiah
nathanjosiah

Reputation: 4459

The error setting for $.ajax will only get called if the actual ajax request has a problem like if the server returns a 404. You would have to return an error in your json and check for it like this:

$('#users').keyup(function(){

    var inpval=$('#users').val();

    $.ajax({
        type: 'POST',
        data: ({p : inpval}),
        url: 'data.php',
        success: function(data) {
              if(!data.error) {
                  $('#output_div').html(data);
              }
              else {
                  // show error
              }
        }
    });
});

And then you would return your normal json when the data is fine and then something like this for the error:

{"error": "Oh NOES!"}

Upvotes: 0

David Gorsline
David Gorsline

Reputation: 5018

Modify your .ajax() call so you can detect error conditions:

        $.ajax({
            type: 'POST',
            data: ({p : inpval}),
            url: 'data.php',
            success: function(data) {
                 $('#output_div').html(data);

            },
            error: function (jqXHR, textStatus, errorThrown) {
                console.log(errorThrown);
            }
        });

This is just to get you started: see http://api.jquery.com/jQuery.ajax/ for more details on what you can do with the error handler.

Of course, it's possible that you're getting a good HTTP status from the call, but this defensive programming will make sure.

A tool like Firebug running in your browser can also help you detect bad HTTP status code returns.

Upvotes: 1

jarchuleta
jarchuleta

Reputation: 1241

In data.php you have to output the error when the data is invalid.

Upvotes: 0

Related Questions