[英]Send data from php to javascript between separate files
I have one login.php, one main.php, one functions_js.js and one functions_php.php (I use jquery too). 我有一个login.php,一个main.php,一个functions_js.js和一个functions_php.php(我也使用jquery)。
My problem is I need to take two variables from login.php and send it to functions_php.php (ajax, ok that works) but then in functions_php.php I make a query select (with this variables) and it returns me an array which I need on functions_js.js , in this file I have a function to insert images on main.php. 我的问题是我需要从login.php中获取两个变量,并将其发送到functions_php.php(ajax,可以使用),然后在functions_php.php中,我进行查询选择(带有该变量),然后返回一个数组我需要在functions_js.js上,在此文件中,我有一个函数可以在main.php上插入图像。 I don't know how to pass the array from functions_php to functions_js.js.
我不知道如何将数组从functions_php传递给functions_js.js。 I've read that I can use json but I don't know how to use between 2 different files.
我读过我可以使用json,但不知道如何在2个不同的文件之间使用。
I've tried to use json and pass it to main.php but no works: 我试图使用json并将其传递给main.php,但是没有用:
functions_php.php: functions_php.php:
if (isset($_POST['user'])) {
function startPerson($nick,$pass){
$query = $conexion->query('SELECT archive_id FROM family WHERE family_id=(select distinct family_id from person where user_id = (select user_id from user where user_name="'.$nick.'" and password="'.$pass.'"))');
$query->execute();
while ($res = $query->fetch()) {
$id = $res['archive_id']; //archive_id
}
//coger las personas de las familias de ese archive_id
$query = $conexion->query('SELECT person_id, birth_date FROM person WHERE family_id in (select family_id from family where archive_id='.$id.')');
$query->execute();
while ($res = $query->fetch()) {
$person[] = $res[0]; //archive_id
$date[]=$res[1];
}
return array($person, $date);
}
header('Content-Type: application/json');
$encoded=json_encode(startPerson($_POST['user'],$_POST['pass']));
echo $encoded;
}
main.php: main.php:
<?php
$json_data=json_encode($encoded);
?>
<script>
var an_obj= <?php echo $json_data;?> ;
alert(an_obj);//returns null
</script>
function_js.js: function_js.js:
function startPerson (date) {
for(i=0;i=>date.length;i++){
var elem = date[i].split('-');
var mon= elem[1]; //month
var year=elem[0]; //year
var mont= num2mon(mon);
position(year,mont);
elem=" ";
mon=" ";
year=" ";
}
}
function position(year, mon)
{
$('#' + year + ' .' + mon).prepend('<img class="black_point" src="./images/circle.png"/>');
}
Have you tried changing: 您是否尝试过更改:
var an_obj= <?php echo $json_data;?> ;
to: 至:
var an_obj = "<?php echo $json_data;?>";
Remember, the variable is a string. 请记住,变量是一个字符串。
Can you also try doing an echo on $encoded ? 您还可以尝试对$ encoded进行回显吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.