Passing array values to js

后端 未结 2 423
难免孤独
难免孤独 2021-01-25 13:16

So current setup is as following:

PHP:data.php



        
相关标签:
2条回答
  • 2021-01-25 13:30

    You can use a hidden field:

    <input id="my-data" type="hidden" value='<?php echo json_encode($data)?>' />
    

    And then you can parse the input value from javascript:

    var data = $.parseJSON($('#my-data').val());
    
    0 讨论(0)
  • 2021-01-25 13:32

    You can use PHP to create the Javascript in the original page. json_encode() can be used to convert a PHP value to the analogous JS literal.

    <?php
      $data = array(
        "16508",
        "16498",
        "16506" 
       );   
    ?>
    <script>
    var data = <?php echo json_encode($data); ?>;
    </script>
    
    0 讨论(0)
提交回复
热议问题