Php 返回数组
很抱歉标题不好,但我不知道如何命名。我的问题是,每当我从一个选择框中传递一个值时,我就会触发这个jquery事件,以便选中复选框。Bassily I echo$res[];在selecctedgr.php。我需要使用json吗?我该怎么做 主页:Php 返回数组,php,jquery,arrays,json,.post,Php,Jquery,Arrays,Json,.post,很抱歉标题不好,但我不知道如何命名。我的问题是,每当我从一个选择框中传递一个值时,我就会触发这个jquery事件,以便选中复选框。Bassily I echo$res[];在selecctedgr.php。我需要使用json吗?我该怎么做 主页: $("#group_name").change(function(){ var groupname = $("#group_name").val(); var selectedGroup = 'gr_name='+ groupname;
$("#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
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[];
?>
更改PHP示例echo$res[]中的最后一行;致: 手册页会告诉你更多 正如@Unicron所说,在将$gr_name变量传递给SQL语句之前,您需要验证该变量 您可以使用:
if(isset($_POST['gr_name'])) {
$gr_name = mysql_real_escape_string($_POST['gr_name']);
}
请参阅:有关PHP手册中的更多信息。如果要使用JSON,只需使用echo JSON_encode$res; 但我不太明白如果代码现在正常工作,您会得到什么,因为您仍然需要在Javascript中进行一些处理来处理结果。您可以使用函数将任意数据转换为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
?>
在客户端,您可以使用,如下所示:
<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>
我发现我的主要问题如下 而不是以前:
$.post("selectedgr.php", {data: selectedGroup}, function(data){
在以下情况下执行此操作:
$.post("selectedgr.php", selectedGroup, function(data){
原谅我的过错。啊,伙计们,关于mysql上的转义,实际上组名不是任何输入字段,而是一个选择框。感谢您的评论、建议和指导
Eric.您的代码有漏洞,其中??请指导我>@Eric您需要使用mysql对$gr\u name进行转义\u real\u escape\u string如果它是字符串,那么您需要添加引号或如果它是数字id,请使用intval检查它是否是数字。`$.eachdata,函数{$+this.attrched,checked;}的用途是什么`$sqlgr=从PRIVILLAGE中选择*,其中MAINGR\u ID=.$gr\u name;在这里您应该使用sql占位符绑定变量。类似这样的内容:$sql=SELECT*FROM PRIVILLAGE,其中MAINGR\u ID=:n$stmt=$pdo->prepare$sql;//将php变量绑定到语句$stmt->bindParam':n',$gr_name$stmt->execute//对不起,我不是php大师;但是你有一个想法……如果你想使用JSONDon,那就是我的建议。不要因为它是一个选择框,就认为有人不会尝试发布你不期望的值。firefox有很多插件可以更改表单中的字段类型,有些插件可以编写bot,向AJAX端点发出CURL请求,或者编写自己的HTML表单提交到PHP中。验证一切!好的,明白了,会逃走的~Thx
$.post("selectedgr.php", selectedGroup, function(data){