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

将数组从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条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路