tags:

views:

544

answers:

2

A couple of questions, first with this var string below, the 1 one works but I need to get the second one working, there is a snytax error because I am not sure how to write it

var string = 'id='+ id ;
var string = 'id='+ id 'USER=1';

Second; This ajax call below, it post to delete.php to delete a comment, it works but I would like to add in some error handling. Once I add it into the backend, how can I make it show an error in jquery ?

$.ajax({
   type: "POST",
   url: "delete.php",
   data: string,
   cache: false,
   success: function(){
    commentContainer.slideUp('slow', function() {$(this).remove();});
    $('#load').fadeOut();
  }
+9  A: 

You're missing a concatenation operator and an ampersand, also I wouldn't use 'string' as a variable name:

var str = 'id='+ id + '&USER=1';

That said, 'string' is not a JS reserved word, but still seems like bad practice (to me at least).

What you are passing in your 'string' variable to $.ajax is a query string, I would suggest reading this for more information:

http://en.wikipedia.org/wiki/Query_string

On your question about error handling, one way is to check the response text for something and act accordingly, e.g.:

$.ajax({
   type: "POST",
   url: "delete.php",
   data: string,
   cache: false,
   success: function(resp){
    if(resp == 'error') { //get PHP to echo the string 'error' if something bad happened
        alert('There was an error!');
    } else {
        commentContainer.slideUp('slow', function() {
            $(this).remove();
        });
        $('#load').fadeOut();   
    }
   }
  });
karim79
that looks like it would work perfect, I get 1 error right now though missing } after the property list, and here is what is on the line with the error = });
jasondavis
@jasondavis - the success block was not closed properly - I've edited, please try again.
karim79
got it now thank you!
jasondavis
A: 

For the second question, do you mean an HTTP error or an error in the server side code?

If you mean an HTTP error, just like "success" there is a "error" that can be called

See the docs here

Chris Thompson
I mean my own errors, like if a user post to many comments in x amount of time, I would like to send an error code back to jquery to the browser to show a message instead of it returning as success
jasondavis