tags:

views:

19

answers:

2

What I want to do is alert() an error message to the user if my PHP script returns an error. Can someone tell me how to do this?

A: 

The ajax has a error block , you can put your custom message in the error block.

gov
http://api.jquery.com/jQuery.ajax/
gov
OK, thanks for the answer. Are you talking about `error:`?
jim
I'm brand new to jquery and I have no idea how this should be done
jim
Yes. Otherwise if the server throws error , the server can send the response code to the UI. based on the response code you can display custom message
gov
Thanks Gov. Let me ask you: prodigitalson provided an example but I'm not entirely clear about it. With his example, I do not need the server error codes, do I? Can I just return false from my PHP function and then alert an error?
jim
A: 

EDIT:

So what i would do is simply use json and return a 200 response with a message for example

PHP

  $validationErrors = array();

  foreach($data as $name => $value){
    // validate each value, if its not valid append a message to the $validationErrors array
    // like $validationErrors[$name] = $message; 
  }

  if(!empty($validationErrors)){
     // send json headers so jquery can parse properly
     header('Content-type: application/json');
     echo json_encode('valid'=>false, 'errors' => $validationErrors));
     exit;
  } else {
    // send json headers so jquery can parse properly
    header('Content-type: application/json');
    echo json_encode(array('valid'=>true));
    exit;
  }

jQuery:

  $.ajax({
      url: '/url/for/whatever.php',
      data: {'my': 'data'},
      dataType: 'json',
      success: function(response){
         if(!response.valid){
            $.each(response.errors, function(name, msg){
                var errmsg = $('<div class="error"></div>').text(msg);
                $('[name='+name+']').before(errmsg);
            });
            alert('Errors were found!'); 
         }
      }
    });

This way you can also display the errors for each field allowing the user to correct them. Youll probably need to modify my error message insertion but you get the idea.


Assuming you mean using $.ajax you would simple assign an error function:

$.ajax({
  url: '/url/for/whatever.php',
  data: {'my': 'data'},
  error: function(){
    alert('An error occurred!');
  }
  success: function(response){
     // whatever happens if the response is successful
  }
});

However for the error function to be invoked the server must respond with 400 or 500 range error code. IF for example you are handling the errors in your php and then outputting an error message then presumable you would be sending a 200 response so the success function would still be invoked. You can manipulate this by setting the header manually with the php header() function if you want.

prodigitalson
@prodigitalson, I like your screen name. Thanks for the code. I already have the `error:` in there but it only seems to work for HTTP server codes. What I need is if my PHP function returns false, an alert to tell the user that their validation didn't pass
jim
Ahhhhh.... I didn't read your entire post. So I can send the header manually.
jim
What server code should I use if want to alert the user to a user error?
jim
See my edit. Its better to just keep everything at a 200 status and simply detect int he response if there are errors that arent actually a failure of the script itself.
prodigitalson
I agree, this is a much better way of doing it. Thank you. I'm going to play around with this. Does this example work?
jim
It should work for the most part... you may need to change the selector for your form fields when you go to insert the errors if you have multiple fields with the same name ona given page... but other than that it should work unless ive made general syntax errors somewhere. If you have any issues you can ask another question with the code that youre using and i or someone else cna help you debug.
prodigitalson
@prodigitalson, Hey, thanks a lot! I really appreciate the help. I'm sure I'll be back soon. :)
jim
Not i did just add a missing comma and changed the js a bit.... ;-)
prodigitalson
Thanks, I was already coming back to say that it wasn't working. lol. I am trying to integrate this code into a JQ plugin. Before I beat my brains in, and since you are knowledgeable, can you tell me if this is even possible?
jim
I dont see why not...
prodigitalson