dongqishun6409 2010-09-30 19:53
浏览 36
已采纳

从JavaScript访问PHP数组

I have the following code where I declare a PHP array variable and inside a function, I put some data into the array. I also display buttons mapped to each index of the array that will show the data in the PHP array for that index number.

When testing on a browser, I don't get the right answer. I checked the page source, it had code like data_array = ["<?php echo implode ('',Array); ?>"]; instead of the text from the Array.

What am I doing wrong and what should I do to get the correct output? (BTW, I tried to execute the same without declaring the function and it seemed to work, but I need a function for my work and can't take that approach).

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
<html lang="en">
    <head>
        <title>Example</title>

        <?php
            $giant_says = array();

            function display()  {
                global $giant_says;

                $giant_says[] = "<a href='http://www.google.com'>Google</a>";
                $giant_says[] = "Yahoo!";
                $giant_says[] = "Bing";

                echo "<div id='content'>";
                echo $giant_says[0];
                echo "</div><br><br>";

                $i = 0;
                while($i < count($giant_says))  {
                    echo "<input type='button' value='".$i."' onClick=\"addtext(".$i.");return false;\"";
                    $i += 1;
                }
            }
        ?>

        <script type="text/javascript">
          function addtext(index) {
              giantSays = ["<?php echo implode ('","', $giant_says); ?>"];
              document.getElementById('content').innerHTML = giantSays[index];
          }
        </script>
    </head>

    <body>
        <?php
            display();
        ?>
    </body>
</html>
  • 写回答

3条回答 默认 最新

  • doufang8282 2010-09-30 20:11
    关注

    You have the order wrong, which is causing the implode() to compress an empty array. I also suggest using json_encode() instead of implode(). It exists for this type of thing - updated example below:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    <html lang="en">
    <head>
    
      <title>Example</title>
    
      <?php
    
      $giant_says = array();
    
      function display(&$giant_says)  {
    
        // Calculate the array (referenced)
    
        $giant_says[] = "<a href='http://www.google.com'>Google</a>";
        $giant_says[] = "Yahoo!";
        $giant_says[] = "Bing";
    
        // Return the HTML, to display later
    
        ob_start();
    
        echo "<div id='content'>";
        echo $giant_says[0];
        echo "</div><br><br>";
    
        $i = 0;
        while($i < count($giant_says))  {
          echo "<input type='button' value='".$i."' onClick=\"addtext(".$i.");return false;\">";
          $i += 1;
        }
        $Return = ob_get_contents();
        ob_end_clean();
        return $Return;
      }
    
      $Display = display($giant_says);
    
      ?>
    
      <script type="text/javascript">
        function addtext(index) {
            giantSays = <?php echo json_encode($giant_says); ?>;
          document.getElementById('content').innerHTML = giantSays[index];
        }
      </script>
    
    </head>
    
    <body>
    
      <?php
        echo $Display;
      ?>
    
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP