1

I've already looked through past questions for an answer to this one, but if I missed something and it does exist, please forgive me.

The essence of what I am trying to do is:

Once a checkbox is clicked, a function is executed.

$(".check_group").live('click', function(){
        var valueid = $(this).val();
                    countThem();
                    if($(this).is(":checked")){
                     blah blah blah.......

The function goes as such:

function countThem(){

        if($('input#currentGroup').val()){

        $.ajax({
            url: "controlBrief.php?ajah=count_group_establishments", 
            dataType: "json", 
            success: function(data){
                $("#message_area").html("There are "+data.totalNumber+" establishments already in this group. <BR/>");
                $("#message_area").slideDown();


            }
        });
        }

    }

The whole thing is successful upto this point, so no problems with the ajax call or with the json encoding from my php page.

The data.totalNumber variable needs to then be returned and saved in a variable to be used either by another function, or within an event. And that is where I am stuck and ask the big HOW? :)

JSON, Ajax and JQuery entered my vocabulary just a month ago, so any direction you can point me to with this one would be great.

user229044
  • 232,980
  • 40
  • 330
  • 338
Nolo
  • 23
  • 3
  • possible duplicate of [jQuery: Return data after ajax call success](http://stackoverflow.com/questions/5316697/jquery-return-data-after-ajax-call-success) – Felix Kling Jun 23 '11 at 21:13
  • Indeed, these are almost the same. Thank you for posting this one. In there I found the exact solution I needed. I didn't know I could return the whole $.ajax call by adding the keyword in front of it! – Nolo Jun 25 '11 at 04:08
  • Well, only if you make the synchronous which you should actually *not do*. You should change your logic to accept callbacks. – Felix Kling Jun 25 '11 at 08:25
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Bergi Apr 22 '13 at 10:52

3 Answers3

1

You could use jQuery.data and attach it to some part of the DOM that makes sense.

NullRef
  • 3,713
  • 1
  • 20
  • 18
1

You could use instead of the general $.ajax method either $.get or $.post with the following syntax:

 $.get("controlBrief.php",{"ajah":"count_group_establishments"},function(data)
 {
        $("#message_area").html("There are "+data.totalNumber+" establishments already in this group.");
        $("#message_area").slideDown();
 },"json");
Pasman
  • 445
  • 3
  • 14
  • This code also works, but it brings me to the same point. How do I then extract the value of data.totalNumber for it to be used elsewhere (within another function, for example)? – Nolo Jun 24 '11 at 04:53
  • @Nolo Well there are several ways depending on the context. You can create a global variable and store the data there or you can better create class containing grouped global data. Another option would be to store the data in DOM as NullRef suggested and take the value from there in the needed function. – Pasman Jun 24 '11 at 13:34
0

How to return an array from jQuery ajax success function properly? is very similar to your problem. That question is about returning an array but your single data.totalNumber can be returned in a similar way.

Community
  • 1
  • 1
andyb
  • 43,435
  • 12
  • 121
  • 150
  • The question was indeed the same, but it complicated things rather than simplify them. I got it solved in the meantime, but not through that suggestion. Thank you though. – Nolo Jun 25 '11 at 04:07
  • Good to read that you sorted it. You can _accept_ any of the answers here if they provided you with a solution. If none of them were satisfactory, it would be very useful for others to read your solution so you could add it as an answer and accept that instead! :-) – andyb Jun 25 '11 at 06:15