嗨我将一个数组从php传递给另一个使用json在javascript函数上使用它,但是没有返回任何内容,一直给我“null”。
php code
function getParents($id_child){
include "conexion.php";
$query = $conexion->query('SELECT person_id FROM children WHERE children_id='.$id_child.';');
$query->execute();
while ($res = $query->fetch()) {
$id_parents[] = $res[0]; //family_id
}
return $id_parents;
echo json_encode($id_parents);
}
getParents($_POST['id_child']);
接受json的Php:
<?php
$json_data=json_encode($id_parents);
?>
function sacarPadres(id_child){
$.ajax({
data: {"id_child" : id_child,
},
url: 'php/functions_sql.php',
type: 'post',
success: function(output) {
}
});
var an_obj= "<?php echo $json_data;?>";
alert(an_obj); // always null
}
<div id="form">
<FORM NAME=form1>
<INPUT TYPE='button' NAME='myFamily' value='add new family' onclick="sacarPadres(70);">
</FORM>
答案 0 :(得分:3)
返回后,一切都消失了
只是remove return type
//return $id_parents;
echo json_encode($id_parents);
<强>更新强>
function getParents($id_child){
include "conexion.php";
$query = $conexion->query('SELECT person_id FROM children WHERE children_id='.$id_child.';');
$query->execute();
while ($res = $query->fetch()) {
$id_parents[] = $res[0]; //family_id
}
return json_encode($id_parents);
}
echo $val=getParents($_POST['id_child']);
<强>的Ajax 强>
$.ajax({
data: {"id_child" : id_child,
},
url: 'php/functions_sql.php',
type: 'post',
success: function(output) {
alert(output);//alert here
}
});
答案 1 :(得分:2)
return
语句导致程序退出该函数。 return
之后的所有语句都不会被执行。将return
移到功能的末尾或完全删除。