简体   繁体   English

如何将PHP数组参数传给Javascript Function?

[英]How to pass PHP array parameter to Javascript Function?

index.php

<script type="text/javascript" src="javascript.js"> </script>
<?php
 $movies = array("Bloodsport", "Kickboxer", "Cyborg", "Timecop", "Universal Soldier", "In Hell", "The Quest");
?>

<input type="submit" value="Test Javascript" onclick="showMovies(<?php echo $movies; ?>);" />


javascript.js

function showMovies(movies) {
 alert(movies.length);

 return false;
}

I am new to programming so Im having hard time fixing this one which is obviously simple for you guys.我是编程新手,所以我很难修复这个对你们来说显然很简单的问题。

When I hit the submit button it says the that the array size is 1 which I think should be 7. How could this be?当我点击提交按钮时,它说数组大小为 1,我认为应该是 7。这怎么可能?

<input type="submit" value="Test Javascript" onclick='showMovies(<?php echo json_encode($movies); ?>);' />

Notice the json_encode , which encodes objects or arrays for Javascript (JSON stands for JavaScript Object Notation) and also notice the ' instead of " , because JSON uses " . Notice the json_encode , which encodes objects or arrays for Javascript (JSON stands for JavaScript Object Notation) and also notice the ' instead of " , because JSON uses " .

Although, this soulution would be better:虽然,这个解决方案会更好:

<script type="text/javascript">
    document.getElementByID('submitButton').onclick = function() {
        var movies = <?php echo json_encode($movies); ?>;
        showMovies(movies);
    };
</script>
...
<input type="submit" value="Test JavaScript" id="submitButton">

Try this尝试这个

PHP Code PHP 代码

<script type="text/javascript" src="javascript.js"> </script>
<?php
  $movies = array("Bloodsport", "Kickboxer", "Cyborg", "Timecop", "Universal Soldier", "In Hell", "The Quest");
  $mov_str = implode(",", $movies);
?>

<input type="submit" value="Test Javascript" onclick="showMovies('<?php echo $mov_str; ?>');" />

In Javascript File在 Javascript 文件中

function showMovies(movies) {
  movies = movies.split(",");
  alert(movies.length);
 return false;
}

Output Output

7

I hope this will help you.我希望这能帮到您。

Encode as JSON before outputting.输出前编码为 JSON。

<input type="submit" value="Test Javascript" onclick="showMovies(<?php echo json_encode($movies); ?>);" />

Your PHP variables only live on the server.您的 PHP 变量仅存在于服务器上。 They are completely separate from the JavaScript variables on the client.它们与客户端上的 JavaScript 变量完全分开。 The only mechanism for passing values from the server to the client is through the contents of the web page (or through specially requested behind-the-scenes web content through AJAX ).将值从服务器传递到客户端的唯一机制是通过 web 页面的内容(或通过AJAX的特别请求的幕后 web 内容)。

This means that to make your JavaScript receive PHP values, you have to write JavaScript with those values embedded inside of it.这意味着要使您的 JavaScript 接收 PHP 值,您必须编写 JavaScript 并将这些值嵌入其中。 You must mix the PHP with the JavaScript at least a tiny bit to get the stuff that runs on the client to have any data from the server.您必须将 PHP 与 JavaScript 混合至少一点点,以使在客户端上运行的东西能够从服务器获取任何数据。

This is how all web server-side scripting works in all languages.这就是所有 web 服务器端脚本在所有语言中的工作方式。

JavaScript simply cannot know what goes in your movies variable unless you stuff it full of values, in JavaScript . JavaScript 根本无法知道您的电影变量中发生了什么,除非您在 JavaScript 中将其填满值。

I recommend you to @levu's answer to see a good way to get your PHP variable's values into JavaScript.我建议您查看@levu 的答案,以了解将 PHP 变量的值转换为 JavaScript 的好方法。

<?php
  $movies = array("Bloodsport", "Kickboxer", "Cyborg", "Timecop", "Universal Soldier", "In Hell", "The Quest");
  $str=implode(",",$movies);
?>
<script type="text/javascript" >
  var arr=new Array();
  var str="<?php echo $str; ?>";
  arr=str.split(',');
  //alert(arr.toSource());
  function showMovies(movies) {
    alert(movies.length);
    return false;
  }
</script>

<input type="submit" value="Test Javascript" onclick="showMovies(arr)" >

Hey Please use above for your desired result.嘿,请在上面使用以获得您想要的结果。 It is tested code.它是经过测试的代码。

You can pass array in js by json_encode() php function.. json_encode() will make array into string.您可以通过 json_encode() 在 js 中传递数组 php function .. json_encode() 会将数组转换为字符串。 you can get array back by saprating that string in js.您可以通过在 js 中删除该字符串来获取数组。

You can also parse an array for JavaScript with json_encode()您还可以使用 json_encode() 解析 JavaScript 的数组

$array = array('1','a','b');

echo '<script type="text/javascript">';
echo 'var x = ' . json_encode($array) . ';';
echo '</script>';

This also works for associative arrays:这也适用于关联 arrays:

$array = array("x" => '1',"y" => 'a',"z" => 'b');
echo '<script type="text/javascript">';
echo 'var x = ' . json_encode($array) . ';';
echo '</script>';

I think it's a good one.我认为这是一个很好的。

onclick="showMovies({{json_encode($movies)}})"

Solved by one of my colleague(Istiaq Harun Shovon)由我的一位同事解决(Istiaq Harun Shovon)

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

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