0

Here's a jQuery-ajax function that returns some data from a php file.

$('.up').click(function() {
    var act = $('.active').val();

    $.ajax({
        type: "POST",
        url: "database.php",
        data: {
            value: act
        }
    }).done(function(result) {
        $('#msg').html(result)
    });
});

I get the array result of the query. I have no problem with.

PHP Result :

array(2) {
  [0]=> array(2) { ["Label"]=> string(3) "Group1" ["Count"]=> string(1) "244" }
  [1]=> array(2) { ["Status"]=> string(7) "Group2" ["Count"]=> string(1) "125" }
}

Actual query result is this.

Label   Count
Group1  244
Group2  125

Now that I get the result. I wanted to store the numbers of the Count column to two different javascript variables.

So far I tried with this.

var value1 = "<?php echo htmlentities($data[0]['Count']) ?>";
var value2 = "<?php echo htmlentities($data[1]['Count']) ?>";

Tried replacing with the above piece of code with result in the ajax call.

But when I did this the click event itself was not triggered.

I've saved the numbers to two php variables namely value1 and value2.

And in the result of the ajax call i get the result like 244125.

Help me saving that number to two different javascript variables.

8
  • 1
    Hey, welcome to Stack Overflow. Please have a look at my edits to your post. You might learn something from them (don't use backticks for non-code or emphasis, no need to use <pre>, there is no such thing called "jquery variable") Commented Dec 31, 2013 at 12:43
  • @ThiefMaster - Thanks for the edits and suggestions. Commented Dec 31, 2013 at 12:45
  • json_encode is your friend. Commented Dec 31, 2013 at 12:53
  • @moonwave99 - I wanted to store the ajax result to javascript variables. Using json_encode how will it help me? Commented Dec 31, 2013 at 12:56
  • The php output should be formatted in a way javascript can easily read and understand. e.g JSON or XML. Commented Dec 31, 2013 at 12:56

1 Answer 1

0

I am assuming you are going to do something with this data so you'll need to know what you are looking for.

Get your PHP to serve up JSON with keys like so:

{"Bob": "24", "Sue": "12"}

This way you can access the information you need or just loop through the whole lot. I've made a little fiddle which I am hoping will help you understand.

Ultimately you simply want to use the jQuery $.getJSON to pull the information and then deal with it however you please.

http://jsfiddle.net/PgVkL/

If you need PHP to output in the format you specified then you can do:

$people = array("24", "12", "16", "48");
var_dump(json_encode($people));

Here's the docs: https://www.php.net/json_encode

Sign up to request clarification or add additional context in comments.

2 Comments

Happy New Year. And thank you very much for the reply. {"Bob": "24", "Sue": "12"} I can convert my sql query result to this json format. But what i actually i need it. I wanted to convert this json {"Bob": "24", "Sue": "12"} to this format [24, 12]. How will I do it?
Do you mean how to get the PHP to do this? Also if you do it this way you have no reference to what the data related to - it becomes data and not information?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.