php Jquery .post() 返回数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5375542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery .post() return array
提问by Eric T
Sorry for the bad title, but I don't know how to name this. My problem is that whenever I pass a value from a select box I trigger this jquery event in order to check on the check boxes. Bassically I echo $res[]; at selecctedgr.php. Do I need to use json? and how can I do this?
对不起,标题不好,但我不知道如何命名。我的问题是,每当我从选择框中传递值时,我都会触发此 jquery 事件以检查复选框。基本上我 echo $res[]; 在selectedgr.php。我需要使用 json 吗?我该怎么做?
Mainpage:
主页:
$("#group_name").change(function(){
var groupname = $("#group_name").val();
var selectedGroup = 'gr_name='+ groupname;
$.post("selectedgr.php", {data: selectedGroup}, function(data){
$.each(data, function(){
$("#" + this).attr("checked","checked");
});
},"json");
});
PHP (selectedgr.php):
PHP (selectedgr.php):
<?php
include_once '../include/lib.php';
$gr_name=mysql_real_escape_string($_POST['gr_name']);
$sqlgr = "SELECT * FROM PRIVILLAGE WHERE MAINGR_ID=".$gr_name;
$resultgr = sql($sqlgr);
while($rowgr = mysql_fetch_array($resultgr)){
$res[] = $rowgr['ACT_ID'];
}
echo $res[];
?>
回答by Treffynnon
Change the last line in your PHP sample (echo $res[];
) to:
将 PHP 示例 ( echo $res[];
) 中的最后一行更改为:
echo json_encode($res);
json_encode()
manual page will tell you more.
json_encode()
手册页会告诉你更多。
Also as @Unicron says you need to validate the $gr_name variable before passing it to your SQL statement.
同样正如@Unicron 所说,您需要先验证 $gr_name 变量,然后再将其传递给您的 SQL 语句。
You could use:
你可以使用:
if(isset($_POST['gr_name'])) {
$gr_name = mysql_real_escape_string($_POST['gr_name']);
}
See: http://php.net/manual/en/function.mysql-real-escape-string.phpfor more information in the PHP manual.
有关PHP 手册中的更多信息,请参阅:http: //php.net/manual/en/function.mysql-real-escape-string.php。
回答by Salman A
You can use json_encodefunction to convert arbitrary data into JSON. Assuming that you want to return an array of strings, here is how you will use json_encode:
您可以使用json_encode函数将任意数据转换为 JSON。假设你想返回一个字符串数组,下面是你将如何使用 json_encode:
<?php
include_once '../include/lib.php';
$res = array(); // initialize variables
$sqlgr = sprintf("
SELECT ACT_ID
FROM PRIVILLAGE
WHERE MAINGR_ID=%d
",
$_POST['gr_name']
); // only select those columns that you need
// and do not trust user input
$resultgr = sql($sqlgr);
while($rowgr = mysql_fetch_array($resultgr)){
$res[] = $rowgr['ACT_ID'];
}
echo json_encode($res); // use json_encode to convert the PHP array into a JSON object
// this will output something like ['foo', 'bar', 'blah', 'baz'] as a string
?>
On the client side you can use jQuery.post method, like this:
在客户端,您可以使用jQuery.post 方法,如下所示:
<script type="text/javascript">
$("#group_name").change(function () {
$.post("selectedgr.php", {
gr_name: $(this).val()
}, function (data) {
// console.log(data);
// jQuery will convert the string "['foo', 'bar', 'blah', 'baz']" into a JavaScript object
// (an array in this case) and pass as the first parameter
for(var i = 0; i < data.length; i++) {
$("#" + data[i]).attr("checked", "checked");
}
}, "json");
});
</script>
回答by Michael Laffargue
If you want to use JSON then just use echo json_encode($res);
But I don't really understand what you'll gain if your code is working now, since you'll still have to do some processing in the Javascript to handle the result.
如果您想使用 JSON,则只需使用echo json_encode($res);
但我真的不明白如果您的代码现在可以工作,您会获得什么,因为您仍然需要在 Javascript 中进行一些处理来处理结果。
回答by Eric T
I found my major problem as below
我发现我的主要问题如下
instead of (before):
而不是(之前):
$.post("selectedgr.php", {data: selectedGroup}, function(data){
do this (after):
这样做(之后):
$.post("selectedgr.php", selectedGroup, function(data){
Forgive my bad. Ahh ya guys, regarding the escaping on mysql actually #group_name
is not any input field but a select box. Appreciate for every comment, suggestion and guide.
原谅我的不好。啊,伙计们,关于 mysql 的转义实际上#group_name
不是任何输入字段,而是一个选择框。感谢您的每一条评论、建议和指导。
Eric.
埃里克。