I have a PHP script that retrieves names (strings) from database. I would like to pass this array to Javascript using $.ajax(). I cannot understand how should I encode the array in PHP and then decode it in Javascript. Could someone give an example code for this ? Thanks a lot !!
我有一个PHP脚本,它从数据库中检索名称(字符串)。我想使用$.ajax()将这个数组传递给Javascript。我不明白如何用PHP编码数组,然后用Javascript解码数组。有人能给出一个例子代码吗?非常感谢! !
5 个解决方案
#1
3
<?php // test.php
$myArray = array(1, 2, 3);
echo json_encode($myArray);
?>
HTML File:
HTML文件:
$(function() {
$.getJSON('http://localhost/test.php', function(data) {
$(data).each(function(key, value) {
// Will alert 1, 2 and 3
alert(value);
});
});
});
#2
2
u can use json_encode
你可以使用json_encode
<?php
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);
?>
full example you can read at :
完整的例子,你可以在:
http://www.prodevtips.com/2008/08/15/jquery-json-with-php-json_encode-and-json_decode/
http://www.prodevtips.com/2008/08/15/jquery-json-with-php-json_encode-and-json_decode/
or
或
http://www.prodevtips.com/2009/12/09/multiple-select-lists-with-jquery-and-json/
http://www.prodevtips.com/2009/12/09/multiple-select-lists-with-jquery-and-json/
#3
1
<?php
echo json_encode(array('key' => 'value', 'cool' => 'ice'));
?>
json is a javascript object. So there is no need to "decode" it. However, it looks like you are using jquery. There is a nifty function for retrieving json data:
json是一个javascript对象。所以没有必要去“解码”它。但是,看起来您正在使用jquery。有一个漂亮的函数,用于检索json数据:
jQuery.getJSON(url, senddata, function(returndata){alert(returndata.cool);})
or
或
jQuery.getJSON(url, senddata, function(returndata){mybigfunction(returndata);})
mybigfunction(data)
{
myvar = data.cool;
alert(myvar);
}
http://api.jquery.com/jQuery.getJSON/
http://api.jquery.com/jQuery.getJSON/
or you could also do it with $.ajax as you mentioned:
或者你也可以用$来做。ajax是你提到的:
jQuery.ajax({
url: url,
dataType: 'json',
data: senddata,
success: function(data){mybigfunction(data)}
});
mybigfunction(data)
{
myvar = data.cool;
alert(myvar);
}
http://api.jquery.com/jQuery.ajax/
http://api.jquery.com/jQuery.ajax/
The "callback" is a function that gets called and passed the json data returned from the url.
“回调”是一个函数,它被调用并传递从url返回的json数据。
You will 'ice' baby... ermm... sorry for the corn.
你会“冰”婴儿……这里好吃……对不起,玉米。
The getJSON method is rather short and handy. Have a look at the links for more details.
getJSON方法非常简短和方便。请查看链接以了解更多细节。
#4
0
To do this, you'll just have to echo out a script into the PHP page that contains your data, which you can then access from any other Javascript on the page, including jQuery and .ajax().
为此,您只需将一个脚本回显到包含数据的PHP页面中,然后您可以从页面上的任何其他Javascript(包括jQuery和.ajax())访问该页面。
Again, if you just want to pass it via an AJAX call, just use json_encode():
同样,如果您只想通过AJAX调用传递它,只需使用json_encode():
<?php
echo json_encode(
array(
'groupidlist'=>$groupids,
'groupnamelist'=>$groupnames,
'serverurl'=>$serverurl,
'uid'=>$curuser->getID()
)
);
?>
And then process it with the callback functions from .ajax()
or, probably better, .getJSON()
, which is built for just this use.
然后使用.ajax()或更好的. getjson()的回调函数对其进行处理,该函数就是为此而构建的。
I promise I don't just spam my blog here, but I wrote a post on passing variables between Javascript and PHP, because I did it often enough that I came up with a simple/reliable/clean and reusable way to do so. If you're regularly passing data from PHP to Javascript and don't need AJAX, I'll paste the essentials here:
我保证我不会在这里发垃圾邮件给我的博客,但是我写了一篇关于在Javascript和PHP之间传递变量的文章,因为我经常这么做,所以我想出了一个简单/可靠/干净和可重用的方法。如果您经常将数据从PHP传递到Javascript,并且不需要AJAX,那么我将在这里粘贴一些要点:
At the top of each external js file, I add comments as to which PHP variables are required, so I can keep track of what I need when I include it (this is optional, of course, but nice):
在每个外部js文件的顶部,我添加了关于哪些PHP变量是必需的注释,以便在包含这些变量时跟踪需要的内容(当然,这是可选的,但很好):
/* This script depends on the following variables in phpvars:
groupidlist
groupnamelist
serverurl
uid
*/
Then, in the PHP file, I pass the needed variables with a single line of Javascript, assigning a JSON Array with all the needed values. Examples in PHP, directly from my code:
然后,在PHP文件中,我使用一行Javascript传递所需的变量,并为JSON数组分配所需的所有值。PHP示例,直接从我的代码:
<script type="text/javascript">
var phpvars = <?php
echo json_encode(
array(
'groupidlist'=>$groupids,
'groupnamelist'=>$groupnames,
'serverurl'=>$serverurl,
'uid'=>$curuser->getID()
)
);
?>;
</script>
Once that is set up, I can then simply access whatever PHP Variables I need in the Javascript through the phpvars
array. For example, if I needed to set an image source using my serverurl, I could do as follows:
设置好之后,我就可以通过phpvars数组访问Javascript中需要的任何PHP变量。例如,如果我需要使用我的serverurl设置一个图像源,我可以做如下:
imgElement.src = phpvars.serverurl + '/images/example.png';
Because it uses JSON, there is no worrying about making sure that you don't screw anything up in your Javascript by trying to insert the PHP variables. The encoding/decoding of the variables is handled on both ends by built-in JSON functions, so it is very hard to break it, and brainless to pass variables - you pass them like you would any other PHP array. In my fiddling that led to this, I had problems with both of these, and this solution takes care of them nicely.
因为它使用的是JSON,所以无需担心插入PHP变量会导致Javascript中的任何错误。变量的编码/解码都是通过内置的JSON函数来处理的,所以很难打破它,传递变量也很不明智,就像传递其他PHP数组一样。在我的处理过程中,我遇到了这两个问题,这个解决方案很好地解决了它们。
#5
0
This is Php File Code
这是Php文件代码
<?php
$array = array(1, 2, 3);
echo json_encode($array);
?>
Then you can parse $array
in your $.ajax()
like this
然后可以像这样解析$.ajax()中的$数组
success: function (data) {
var x = JSON.parse(data);
console.log(x);
}
#1
3
<?php // test.php
$myArray = array(1, 2, 3);
echo json_encode($myArray);
?>
HTML File:
HTML文件:
$(function() {
$.getJSON('http://localhost/test.php', function(data) {
$(data).each(function(key, value) {
// Will alert 1, 2 and 3
alert(value);
});
});
});
#2
2
u can use json_encode
你可以使用json_encode
<?php
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);
?>
full example you can read at :
完整的例子,你可以在:
http://www.prodevtips.com/2008/08/15/jquery-json-with-php-json_encode-and-json_decode/
http://www.prodevtips.com/2008/08/15/jquery-json-with-php-json_encode-and-json_decode/
or
或
http://www.prodevtips.com/2009/12/09/multiple-select-lists-with-jquery-and-json/
http://www.prodevtips.com/2009/12/09/multiple-select-lists-with-jquery-and-json/
#3
1
<?php
echo json_encode(array('key' => 'value', 'cool' => 'ice'));
?>
json is a javascript object. So there is no need to "decode" it. However, it looks like you are using jquery. There is a nifty function for retrieving json data:
json是一个javascript对象。所以没有必要去“解码”它。但是,看起来您正在使用jquery。有一个漂亮的函数,用于检索json数据:
jQuery.getJSON(url, senddata, function(returndata){alert(returndata.cool);})
or
或
jQuery.getJSON(url, senddata, function(returndata){mybigfunction(returndata);})
mybigfunction(data)
{
myvar = data.cool;
alert(myvar);
}
http://api.jquery.com/jQuery.getJSON/
http://api.jquery.com/jQuery.getJSON/
or you could also do it with $.ajax as you mentioned:
或者你也可以用$来做。ajax是你提到的:
jQuery.ajax({
url: url,
dataType: 'json',
data: senddata,
success: function(data){mybigfunction(data)}
});
mybigfunction(data)
{
myvar = data.cool;
alert(myvar);
}
http://api.jquery.com/jQuery.ajax/
http://api.jquery.com/jQuery.ajax/
The "callback" is a function that gets called and passed the json data returned from the url.
“回调”是一个函数,它被调用并传递从url返回的json数据。
You will 'ice' baby... ermm... sorry for the corn.
你会“冰”婴儿……这里好吃……对不起,玉米。
The getJSON method is rather short and handy. Have a look at the links for more details.
getJSON方法非常简短和方便。请查看链接以了解更多细节。
#4
0
To do this, you'll just have to echo out a script into the PHP page that contains your data, which you can then access from any other Javascript on the page, including jQuery and .ajax().
为此,您只需将一个脚本回显到包含数据的PHP页面中,然后您可以从页面上的任何其他Javascript(包括jQuery和.ajax())访问该页面。
Again, if you just want to pass it via an AJAX call, just use json_encode():
同样,如果您只想通过AJAX调用传递它,只需使用json_encode():
<?php
echo json_encode(
array(
'groupidlist'=>$groupids,
'groupnamelist'=>$groupnames,
'serverurl'=>$serverurl,
'uid'=>$curuser->getID()
)
);
?>
And then process it with the callback functions from .ajax()
or, probably better, .getJSON()
, which is built for just this use.
然后使用.ajax()或更好的. getjson()的回调函数对其进行处理,该函数就是为此而构建的。
I promise I don't just spam my blog here, but I wrote a post on passing variables between Javascript and PHP, because I did it often enough that I came up with a simple/reliable/clean and reusable way to do so. If you're regularly passing data from PHP to Javascript and don't need AJAX, I'll paste the essentials here:
我保证我不会在这里发垃圾邮件给我的博客,但是我写了一篇关于在Javascript和PHP之间传递变量的文章,因为我经常这么做,所以我想出了一个简单/可靠/干净和可重用的方法。如果您经常将数据从PHP传递到Javascript,并且不需要AJAX,那么我将在这里粘贴一些要点:
At the top of each external js file, I add comments as to which PHP variables are required, so I can keep track of what I need when I include it (this is optional, of course, but nice):
在每个外部js文件的顶部,我添加了关于哪些PHP变量是必需的注释,以便在包含这些变量时跟踪需要的内容(当然,这是可选的,但很好):
/* This script depends on the following variables in phpvars:
groupidlist
groupnamelist
serverurl
uid
*/
Then, in the PHP file, I pass the needed variables with a single line of Javascript, assigning a JSON Array with all the needed values. Examples in PHP, directly from my code:
然后,在PHP文件中,我使用一行Javascript传递所需的变量,并为JSON数组分配所需的所有值。PHP示例,直接从我的代码:
<script type="text/javascript">
var phpvars = <?php
echo json_encode(
array(
'groupidlist'=>$groupids,
'groupnamelist'=>$groupnames,
'serverurl'=>$serverurl,
'uid'=>$curuser->getID()
)
);
?>;
</script>
Once that is set up, I can then simply access whatever PHP Variables I need in the Javascript through the phpvars
array. For example, if I needed to set an image source using my serverurl, I could do as follows:
设置好之后,我就可以通过phpvars数组访问Javascript中需要的任何PHP变量。例如,如果我需要使用我的serverurl设置一个图像源,我可以做如下:
imgElement.src = phpvars.serverurl + '/images/example.png';
Because it uses JSON, there is no worrying about making sure that you don't screw anything up in your Javascript by trying to insert the PHP variables. The encoding/decoding of the variables is handled on both ends by built-in JSON functions, so it is very hard to break it, and brainless to pass variables - you pass them like you would any other PHP array. In my fiddling that led to this, I had problems with both of these, and this solution takes care of them nicely.
因为它使用的是JSON,所以无需担心插入PHP变量会导致Javascript中的任何错误。变量的编码/解码都是通过内置的JSON函数来处理的,所以很难打破它,传递变量也很不明智,就像传递其他PHP数组一样。在我的处理过程中,我遇到了这两个问题,这个解决方案很好地解决了它们。
#5
0
This is Php File Code
这是Php文件代码
<?php
$array = array(1, 2, 3);
echo json_encode($array);
?>
Then you can parse $array
in your $.ajax()
like this
然后可以像这样解析$.ajax()中的$数组
success: function (data) {
var x = JSON.parse(data);
console.log(x);
}