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条)

报告相同问题?

悬赏问题

  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺