dongxing1965 2015-06-11 21:54
浏览 49

将数据从php发送到不同文件之间的javascript

I have one login.php, one main.php, one functions_js.js and one functions_php.php (I use jquery too).

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. I don't know how to pass the array from functions_php to functions_js.js. I've read that I can use json but I don't know how to use between 2 different files.

I've tried to use json and pass it to main.php but no works:

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:

<?php
$json_data=json_encode($encoded);
?>
<script>
var an_obj= <?php echo $json_data;?> ;
alert(an_obj);//returns null
</script>

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"/>');    
}
  • 写回答

1条回答 默认 最新

  • douzhuochao4027 2015-06-11 22:12
    关注

    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?

    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端