存储一个AJAX查询结果JavaScript变量的每个值
下面是一个jQuery Ajax的功能从PHP文件返回一些数据。
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结果:
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
现在,我得到的结果。我想的计数
列中的数字存储到两个不同的JavaScript变量。
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']) ?>";
试图与上述一块code替换为结果
在Ajax调用。
但是,当我这样做的单击事件本身不会被触发。
But when I did this the click event itself was not triggered.
我已经保存了数以两个 PHP
变量即值1
和值2
。
I've saved the numbers to two php
variables namely value1
and value2
.
和在结果中的 AJAX
拨打我得到的结果类似 244125
。
And in the result of the ajax
call i get the result like 244125
.
帮我节省了数以两个不同的的JavaScript
变量。
Help me saving that number to two different javascript
variables.
我假设你会做一些与此数据,因此你需要知道你正在寻找什么。
I am assuming you are going to do something with this data so you'll need to know what you are looking for.
让你的PHP来提供JSON有像这样的键:
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.
最后你只是想使用jQuery的$ .getJSON拉的信息,然后解决它,但是你请。
Ultimately you simply want to use the jQuery $.getJSON to pull the information and then deal with it however you please.
如果您需要PHP来输出指定然后格式,你可以这样做:
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));