简体   繁体   English

使用json_encode通过onClick()将字符串数组传递给javascript

[英]Pass an array of strings to javascript via onClick() using json_encode

** Update - While my solution works (and I haven't yet heard why not to use htmlspeciatchars()), I am including a modified version of Jacob Mouka's creative solution which successfully avoids having to use it. **更新-虽然我的解决方案有效(并且我还没有听说为什么不使用htmlspeciatchars()),但我包括Jacob Mouka创造性解决方案的修改版本,该版本成功避免了使用它。 ** **

I am trying to pass an array of strings to javascript via onClick() using json_encode. 我试图使用json_encode通过onClick()将字符串数组传递给javascript。 Just passing json_encode($array) did not work. 仅传递json_encode($ array)不起作用。 I surmised that since json_encode($array) returns ["a","b","c"], the quotes were the issue. 我推测由于json_encode($ array)返回[“ a”,“ b”,“ c”],引号是问题所在。 I was successful with wrapping the json_encode($array) with htmlentities() and then using JSON.parse(array) to turn the string back into an array. 我成功地用htmlentities()包装了json_encode($ array),然后使用JSON.parse(array)将字符串转换回数组。

I read all the posts on this site and none showed this combination as a solution and I wonder if I am making it more complicated than it should be. 我阅读了该网站上的所有帖子,但没有一个帖子将此组合作为解决方案,我想知道我是否使它变得比应有的复杂。 Is htmlentities() the correct function to use? htmlentities()是正确使用的函数吗? Is there a simpler way of sending this array from an onclick() to a javascript function? 有没有更简单的方法将此数组从onclick()发送到javascript函数? Thanks in advance. 提前致谢。

Javascript Java脚本

<script>
function shohmultiple(array){
  alert("Aray as string: " + array);
  array = JSON.parse(array)
  for (i=0; i< array.length; i++){ 
    alert(array[i]);
  }
}
</script>

<?php $array=array("a", "b", "c"); ?>

HTML 的HTML

<a href="#" onClick="shohmultiple('<?php echo htmlspecialchars(json_encode($array)) ?>')">Click Here</a>

Modified Solution from Jacob Mouka (for the workflow I am seeking) Jacob Mouka的修改后的解决方案(针对我正在寻找的工作流程)

Javascript Java脚本

<script type="text/javascript">

function shohmultiple (array) {
    for (i=0; i< array.length; i++){ 
        alert(array[i]);
    }
}
</script>

HTML 的HTML

<?php $array = array("a", "b", "c"); ?>

<script>
    // calling this before outputting <a href> works
    <?php  echo "var js_array = " . json_encode($array) . ";"; ?>
</script>

<a href="#" onClick="shohmultiple(js_array);">Click Here</a>

(edit after some testing) (经过一些测试后编辑)

The problem is actually that json_encode uses double quotes, which messes with the inline javascript (it uses double quotes too). 问题实际上是json_encode使用双引号,它与内联javascript混乱(它也使用双引号)。 If there was some way to force json_encode to use single quotes, that would fix it (but I don't think there is). 如果有某种方法可以强制json_encode使用单引号,则可以解决此问题(但我认为没有)。 A cludgy solutions is something like: 一个笨拙的解决方案是这样的:

<script type="text/javascript">
<?php
    $array = array("a", "b", "3");
    echo "var js_array = " . json_encode($array) . ";";
?>
    function shohmultiple (val) {
        window.foo = val;
        console.log('got',val);
    }

</script>


<a href="#" onClick="shohmultiple(js_array);">Click Here</a>

If you want to avoid using htmlspecialchars(), encase your onClick with single quotes then escape the json string quotes. 如果要避免使用htmlspecialchars(),请用单引号括住onClick,然后转义json字符串引号。 This does not ensure the ability to execute (if your json includes single quotes it will break) but it will work for the purposes of this question. 这不能确保执行的能力(如果您的json包含单引号,则会中断),但可以解决该问题。

Try it like this : 像这样尝试:

<a href="#" onClick='shohmultiple(\'<?php echo(json_encode($array)); ?>\')'>Click Here</a>

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

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