doutuo3575 2018-04-15 19:49
浏览 79
已采纳

将数组从php传递给js

I've file with extension php. In here I've read information from Database. And I need to pass this data(associate array) to js, which also include in this php file.

<?php
    require('../db.php');

    function var_dump1($arr) {
        echo '<pre>';
        var_dump($arr);
        echo '</pre>';
    }

    // load data from Database
    $query = R::getAll("SELECT * from questions");
    var_dump1($query);

?>

<script>
const arr = JSON.parse( '<?php echo json_encode($query); ?>' );
console.log(arr);

</script>   

Outputs:

var_dump1($query);

array(5) {
  [0]=>
  array(7) {
    ["id"]=>
    string(1) "1"
    ["question"]=>
    string(143) "The ______________ of a table is used to modify the design of a table, like modifying the name of a field or changing the data type of a field."
    ["answer1"]=>
    string(14) "Datasheet View"
    ["answer2"]=>
    string(11) "Desisn View"
    ["answer3"]=>
    string(10) "Table View"
    ["answer4"]=>
    string(11) "Wizard View"
    ["true_answer"]=>
    string(1) "1"
  }
  [1]=>
  array(7) {
    ["id"]=>
    string(1) "2"
    ["question"]=>
    string(83) "You can select multiple fields of a table in design view by using the ________ key."
    ["answer1"]=>
    string(3) "Alt"
    ["answer2"]=>
    string(8) "Spacebar"
    ["answer3"]=>
    string(5) "Shift"
    ["answer4"]=>
    string(4) "Ctrl"
    ["true_answer"]=>
    string(1) "3"
  }
...

console.log(arr);

Uncaught SyntaxError: missing ) after argument list

Conclusion: On the part of php all good. But when we try to parse this array in js object in js part - We see strange Error.

Questions:

  1. How to load data(For example, associate array, in my case) from php to js. When php and js are in the same extension file(php).
    1. How to load data(For example, associate array, in my case) from php to js. When php and js are in different files. For example We've 1 extension file php, where we've implement logic(With DB, in my case). And we need to import this data to extension file js.
  • 写回答

3条回答 默认 最新

  • dtfpznrbn503027700 2018-04-15 19:59
    关注

    1) All you need to do is this:

    <script>
    var arr = <?php echo json_encode($query);?>; // <-- no quotes, no parsify
    console.log(arr);
    </script>
    

    JSON = JavaScript Object Notation... its already in the format to assign to a variable to use in javascript.

    Although assigning it to a 'const' is ... probably not a good idea since its an object, but its just how I read them, and still perfectly ok: More Info.

    2) If you are calling a php file from js (ala ajax), then you can return raw JSON from the php return, and depending on HOW you made your ajax call (and with what library, like possibly jquery)... you may or may not need to do JSON.parse(returnData). Not enough information provided how you want to do that, to give a solid answer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥15 QFILHelper怎么恢复全字库,提示进程已完成,只能恢复分区文件
  • ¥150 求 《小魔指》街机游戏机整合模拟软件
  • ¥20 你好,我想问下easyExcel下拉多选,或者复选框可以实现吗
  • ¥20 双非跨考工科哪个专业和方向就业前景好?