简体   繁体   English

Json不从PHP接收数据

[英]Json don't receive data from PHP

Hi I pass an array from php to another to used it on a javascript function using json, but not return nothing, gives me "null" all time. 嗨,我将一个数组从php传递到另一个数组,以在使用json的javascript函数上使用它,但未返回任何内容,使我一直处于“空”状态。

php code PHP代码

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']);

Php who takes the json: 谁使用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>

After return type everything is vanish 返回类型后,一切都消失了

just remove return type 只需remove return type

//return $id_parents;
  echo json_encode($id_parents);

Updated 更新

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
              }
    });

The return statement causes the program to exit the function. return语句使程序退出函数。 All statements after return will not be executed. return后的所有语句将不执行。 Move return to the end of the function or remove at all. return移至函数末尾或完全删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM