-1

My code looks like this. The problem is, PHP side does it job and returns right value. But ajax doesn't execute things inside success: function. What am I missing?

AnswerDiv.on("click", ".NotSelectedAnswer", function() {
    var NotSelectedAnswerBtn = $(".NotSelectedAnswer"),
        SelectedAnswerBtn = $(".SelectedAnswer"),
        AnswerDiv = $("div.Answer"),
        querystring="fromID="+SelectedAnswerBtn.data("id")+"&toID="+$(this).data("id")+"&op=SelectAsAnswer";

    $.ajax({
        url: 'processor.php',
        type: "POST",
        dataType: "json",
        data: querystring,
        success: function(data) {
            if(data.status)
            {
                SelectedAnswerBtn.removeClass("SelectedAnswer").addClass("NotSelectedAnswer").button("enable");
                $(this).removeClass(" NotSelectedAnswer").addClass("SelectedAnswer").button("disable");
                $("div.Answer[data-id=" + SelectedAnswerBtn.data("id") + "]").toggleClass("SelectedDiv");
                $("div.Answer[data-id=" + $(this).data("id") + "]").toggleClass("SelectedDiv");
            }
        }
    });

    return false;
});
Sumurai8
  • 18,213
  • 9
  • 58
  • 88
heron
  • 3,337
  • 22
  • 73
  • 142
  • can you check in console – Sender Sep 08 '12 at 11:28
  • @user108 console doesn't give any message http://screencast.com/t/3PUFBLyKV I can see php returned value – heron Sep 08 '12 at 11:31
  • Get [Firebug](https://getfirebug.com/) and check what's wrong with your ajax. hope it helps you – Siamak A.Motlagh Sep 08 '12 at 11:31
  • @Siamak.A.M lol take a look at screenshot. I already have firebug. Otherwise how do I know, what php side returns? – heron Sep 08 '12 at 11:33
  • Does your server send the header `Content-Type: application/json`? – Raffaele Sep 08 '12 at 11:35
  • @epic_syntax In your php page echo something and in your success function alert() or log something. then we can find out the problem is php or jquery. – Siamak A.Motlagh Sep 08 '12 at 11:36
  • }) so put `;` like }); before return false; – Sender Sep 08 '12 at 11:36
  • There is no problem with sending and receiving to/from PHP . it just works. The problem is something different – heron Sep 08 '12 at 11:37
  • @user108 nothing changed – heron Sep 08 '12 at 11:39
  • @epic_syntax tried the header? – Raffaele Sep 08 '12 at 11:40
  • `echo json_encode();` serverside uses so there is no problem. – heron Sep 08 '12 at 11:41
  • so now to see in [fiddler2](http://www.fiddler2.com/fiddler2/) this will give better idea. – Sender Sep 08 '12 at 11:42
  • 1
    @epic_syntax what are you talking about? `json_encode` doesn't send any header. HTTP headers in PHP are sent with the `header()` function, and according to [1](http://stackoverflow.com/questions/249692/jquery-wont-parse-my-json-from-ajax-query) and [2](http://stackoverflow.com/questions/631418/jquery-getjson-ajax-parseerror) this may be your problem. BTW, we can't state that **the function is not executed**: it may simply be a parsing problem, so the `if (data.status)` is evaluated to `false`. You should set a breakpoint on the JS part, or add a simple `console.log(data)` before the `if` block – Raffaele Sep 08 '12 at 11:45
  • @Raffaele console log returns `Object { status=true}` – heron Sep 08 '12 at 11:50

1 Answers1

1

Try to cache $(this) before ajax call

AnswerDiv.on("click", ".NotSelectedAnswer", function() {
    var NotSelectedAnswerBtn = $(".NotSelectedAnswer"),
    SelectedAnswerBtn = $(".SelectedAnswer"),
    AnswerDiv = $("div.Answer"),
    thisElem=$(this),
    querystring="fromID="+SelectedAnswerBtn.data("id")+"&toID="+$(this).data("id")+"&op=SelectAsAnswer";
    $.ajax({
        url: 'processor.php',
        type: "POST",
        dataType: "json",
        data: querystring,
        success: function(data) {
            if(data.status)
            {
                SelectedAnswerBtn.removeClass("SelectedAnswer").addClass("NotSelectedAnswer").button("enable");
                thisElem.removeClass(" NotSelectedAnswer").addClass("SelectedAnswer").button("disable");
                $("div.Answer[data-id=" + SelectedAnswerBtn.data("id") + "]").toggleClass("SelectedDiv");
                $("div.Answer[data-id=" +thisElem.data("id")+ "]").toggleClass("SelectedDiv");
                return false;
            }
        }
    });

});
Tural Ali
  • 19,346
  • 18
  • 69
  • 127