Reputation: 179
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 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']);
Php who takes the json:
<?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>
Upvotes: 0
Views: 52
Reputation: 22532
After return type everything is vanish
just 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
}
});
Upvotes: 3
Reputation: 3723
The return
statement causes the program to exit the function. All statements after return
will not be executed. Move return
to the end of the function or remove at all.
Upvotes: 2