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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀