在jQuery Ajax之后起作用吗?

I am currently trying to make my Ajax to post some informations from a HTML form, into a PHP form.

Well, now I have made difference codes which should be printed out at the "ajax.comment.php" page, telling what happened.

Like if it went successfully or not.

I now want to make my ajax, check what the printed html was. And then handle on that.. Like if (Printed HTML == "1") {Then do something}.

How can I do that?

My following javascript is:

var name = document.comment.name.value;
var email = document.comment.email.value;
var website = document.comment.website.value;
var message = document.comment.message.value;
var id = document.comment.id.value;
$.ajax({
      type: "POST",
      url: "ajax.addcomment.php",
      data: "name="+name+"&email="+email+"&website="+website+"&message="+message+"&id="+id,
      beforeSend: function() {
        // Action before sending data
      },
      success: function(returned_html) {
        // Action after sending data, where the returned_html var is the returned html text by the php
      }
  });

Inside your success function, try this:

success: function(returned_html) {
    var the_result = $.trim(returned_html);
    if(the_result == '1')
    {
        // Do whatever you wanted here.
    }
    else
    {
        // Do something else here...
    }
}

You are almost done. just do what you want in the success callback which you already implemented.

btw, i've serialized your form so that you don't need to take values individually (unless you want to do something with them).

$.ajax({
      type: "POST",
      url: "ajax.addcomment.php",
      data: $('#comment').serialize(),
      success: function(returned_html) {
        if(returned_html == 1){ 
           //lets do our thing
        } else {
           //lets do other things
      }
  });