Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
what have you tried for triggering the error message? –  nathanjosiah Apr 9 '12 at 15:09
    
I tried adding this:- error: function() { alert("There was an error. Try again please!"); } below the success: parameter in the jquery function. –  Daniel Mabinko Apr 9 '12 at 15:15
add comment

3 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
thanks so much!! –  Daniel Mabinko Apr 9 '12 at 15:20
add comment

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

share|improve this answer
add comment

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!"}

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.