0

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.

1
  • I tried adding this:- error: function() { alert("There was an error. Try again please!"); } below the success: parameter in the jquery function. Commented Apr 9, 2012 at 15:15

3 Answers 3

1

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.

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

Comments

0

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

Comments

0

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

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.