如何将 PHP 数组参数传递给 Javascript 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6502107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass PHP array parameter to Javascript Function?
提问by Son of Man
index.php
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
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。这怎么可能?
采纳答案by ErikE
Your PHP variables only live on the server. They are completely separate from the JavaScript variables on the client. 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).
您的 PHP 变量只存在于服务器上。它们与客户端上的 JavaScript 变量完全分开。将值从服务器传递到客户端的唯一机制是通过网页的内容(或通过AJAX特殊请求的幕后网页内容)。
This means that to make your JavaScript receive PHP values, you have to write JavaScript with those values embedded inside of it. 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.
这意味着要让您的 JavaScript 接收 PHP 值,您必须编写 JavaScript,并将这些值嵌入其中。您必须至少将 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 answerto see a good way to get your PHP variable's values into JavaScript.
我建议您查看@levu 的答案,以了解将 PHP 变量的值导入 JavaScript 的好方法。
回答by Florian
<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 "
.
注意json_encode
,它为 Javascript 编码对象或数组(JSON 代表 JavaScript 对象表示法),还要注意'
代替"
,因为 JSON 使用"
.
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">
回答by Gaurav Porwal
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
输出
7
I hope this will help you.
我希望这能帮到您。
回答by Ignacio Vazquez-Abrams
Encode as JSON before outputting.
输出前编码为 JSON。
<input type="submit" value="Test Javascript" onclick="showMovies(<?php echo json_encode($movies); ?>);" />
回答by shankar kumar
<?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.
嘿 请使用上面的以获得您想要的结果。它是经过测试的代码。
回答by Rukmi Patel
You can pass array in js by json_encode() php function.. json_encode() will make array into string. you can get array back by saprating that string in js.
您可以通过 json_encode() php 函数在 js 中传递数组.. json_encode() 会将数组转换为字符串。您可以通过在 js 中分离该字符串来取回数组。
回答by 5442224
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:
这也适用于关联数组:
$array = array("x" => '1',"y" => 'a',"z" => 'b');
echo '<script type="text/javascript">';
echo 'var x = ' . json_encode($array) . ';';
echo '</script>';