doudi8519
2014-05-08 08:31
采纳率: 100%
浏览 53
已采纳

使用JSON将PHP数组传递给javascript

Okay so I know this has been asked before and I've tried the solutions, but for some reason they don't work for me so I'm asking for some help. I haven't used JSON yet so maybe it's something silly but I have no clue...

Here's the code:

<?php
    $array;

    #successful attempt to display array with json_encode in php
    echo json_encode($array);
?>
<html>
<input id="show" type="button" onclick="showArray()" value="showArray">
<divShow>
</divShow>
<script>
function showArray(){
var array = <?php echo json_encode($array); ?>;

    //Failed attempt to display array in the div field show
    document.getElementById("divShow").appendChild(array);

    //Failed attempt to display the array with an alert.
    for(var i=0; i<2; i++){
        alert(array[i]);
    }
};
</script>
</html>

So what do you guys think? Am I missing something? Is it possible that the array was successfully passed to javascript but for some reason won't show?

Thanks,

-Alex

EDIT:

So I'm getting a series of arrays from a text file. I use these arrays as strings to display on the page and then convert them to float arrays. When I echo one of the float arrays such as $Z_Ausmass with:

echo json_encode($Z_Ausmass);

I get [25.39999961853,121.48651123047]. However, when I use the following to display the array through javascript:

function calc(){
var Z_Ausmass = <?php echo json_encode($Z_Ausmass); ?>;
    for(var o=0; o<Z_Ausmass.length; o++){
    var textnode = document.createTextNode(Z_Ausmass[o]);
    document.getElementById("divCalc").appendChild(textnode);
    }
};

it does not work. It's vital I get the float arrays in the script because the script needs to make calculations based on them and then display the calculations to the user.

图片转代码服务由CSDN问答提供 功能建议

好的,所以我知道之前已经问过这个问题并且我已经尝试了解决方案,但由于某种原因他们不知道 为我工作,所以我要求一些帮助。 我还没有使用过JSON所以也许它是愚蠢的但是我没有线索......

这是代码:

 &lt  ;?php 
 $ array; 
 
#成功尝试在php中显示带有json_encode的数组
 echo json_encode($ array); 
?&gt; 
&lt; html&gt; 
&lt; input id =“show”  type =“button”onclick =“showArray()”value =“showArray”&gt; 
&lt; divShow&gt; 
&lt; / divShow&gt; 
&lt; script&gt; 
function showArray(){
var array =&lt;?php echo  json_encode($阵列);  ?&gt;; 
 
 //尝试在div字段中显示数组show 
 document.getElementById(“divShow”)。appendChild(array); 
 
 //尝试显示数组时出现失败 alert。
 for(var i = 0; i&lt; 2; i ++){
 alert(array [i]); 
} 
}; 
&lt; / script&gt; 
&lt; / html&gt; 
 <  / code>  
 
 

那你觉得怎么样? 我错过了什么吗? 是否有可能将数组成功传递给javascript但由于某种原因不会显示?

谢谢,

-Alex

编辑:

所以我从文本文件中获取了一系列数组。 我使用这些数组作为字符串在页面上显示,然后将它们转换为浮点数组。 当我用$ Z_Ausmass回显其中一个浮点数组时:

  echo json_encode($ Z_Ausmass); 
   
 
 <  p>我得到[25.39999961853,121.48651123047]。 但是,当我使用以下内容通过javascript显示数组时: 
 
 
  function calc(){
var Z_Ausmass =&lt;?php echo json_encode($ Z_Ausmass);  ?&gt ;; 
 for(var o = 0; o&lt; Z_Ausmass.length; o ++){
 var textnode = document.createTextNode(Z_Ausmass [o]); 
 document.getElementById(“divCalc”)。appendChild(  textnode); 
} 
}; 
   
 
 

它不起作用。 在脚本中获取浮点数组至关重要,因为脚本需要根据它们进行计算,然后将计算结果显示给用户。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongshi9526 2014-05-08 08:37
    已采纳

    When i execute the code it works ok.

    The first attempt fails because you can't append the complete array. You need to append each element in the array seperately.

    The second attempt works correctly. You only need to remove the first attempt to make it work because the first attempt stops the execution of the javascript.

    edit

    I tried to fix the code for you. I used a simple array with only text. The element you wanted to show in did not have the id you where referencing to

    <divShow></divShow>//wrong
    <div id="divShow"></div>//right
    

    to loop trough the complete array you do not want to hard code the max # of elements use arr.length as max for the 'for'-loop.

    You can't directly append raw text to an html element. You need to make a TextNode of it and then append that node to the html element.

    var textnode=document.createTextNode(arr[i]);
    document.getElementById("divShow").appendChild(textnode);
    

    So The working code will be something like this:

    <?php
    $array = array("test","text","show");
    
    #successful attempt to display array with json_encode in php
    echo json_encode($array);
    ?>
    <html>
    <input id="show" type="button" onclick="showArray()" value="showArray">
    <div id="divShow">
    </div>
    <script>
        function showArray(){
            var arr = <?php echo json_encode($array); ?>;
    
            //Put the text in a text node, append to the div
            for(var i=0; i< arr.length; i++){
                var textnode=document.createTextNode(arr[i]);
                document.getElementById("divShow").appendChild(textnode);
            }
        };
    </script>
    </html>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题