简体   繁体   English

从JavaScript访问PHP数组

[英]Access a PHP array from JavaScript

I have the following code where I declare a PHP array variable and inside a function, I put some data into the array. 我有以下代码,在其中声明PHP数组变量,并在函数内部,将一些数据放入数组中。 I also display buttons mapped to each index of the array that will show the data in the PHP array for that index number. 我还将显示映射到数组每个索引的按钮,这些按钮将显示该索引号在PHP数组中的数据。

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); ?>"]; 我检查了页面源代码,它具有类似data_array = ["<?php echo implode ('',Array); ?>"]; instead of the text from the Array. 而不是来自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>

You have the order wrong, which is causing the implode() to compress an empty array. 您的命令顺序错误,导致implode()压缩一个空数组。 I also suggest using json_encode() instead of implode() . 我还建议使用json_encode()而不是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>

您试图在填充$giant_says数组之前使其内爆( $giant_says爆之后,当调用需要在之前发生时,您要调用display() )。

The problem is that you call the display method, that fills the content after the html part with the javascript is sended. 问题是您调用显示方法,该方法在发送带有javascript的html部分后填充内容。

the html code is "like" making an "echo 'html'" from your php. html代码是“喜欢”的,使您的php产生了“ echo'html'”。 Your html is already processed but the display method is not called. 您的html已被处理,但未调用display方法。 call the method before the html code. 在html代码之前调用该方法。

Example: 例:

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

    function display()  {
        global $giant_says;
        echo '<div id="content">'.$giant_says[0]."</div><br><br>";
        $i = 0;
        while($i < count($giant_says))  {
          echo "<input type='button' value='".$i."' onClick=\"addtext(".$i.");\" />";
          $i += 1;
        }  
    }
?>
<html>
    <head>
        <title>Example</title>
        <script type="text/javascript">
          function addtext(index) {
             giantSays = ["<?php echo implode ('","', $giant_says); ?>"];
             document.getElementById('content').innerHTML = giantSays[index];
             return false;
          }
       </script>
    </head>
    <body>
        <?php  display(); ?>
    </body>
 </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM