如何将PHP数组参数传递给Javascript函数?

时间:2021-08-16 11:57:20

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。这怎么可能呢?

7 个解决方案

#1


1  

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变量。将值从服务器传递到客户机的唯一机制是通过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. 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 answer to see a good way to get your PHP variable's values into JavaScript.

我向@levu推荐一种将PHP变量的值转换为JavaScript的好方法。

#2


8  

<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">

#3


3  

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.

我希望这能对你有所帮助。

#4


2  

Encode as JSON before outputting.

在输出之前编码为JSON。

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

#5


1  

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中的字符串来获取数组。

#6


1  

<?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.

嘿,请使用上面的你想要的结果。这是经过测试的代码。

#7


0  

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>';

#1


1  

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变量。将值从服务器传递到客户机的唯一机制是通过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. 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 answer to see a good way to get your PHP variable's values into JavaScript.

我向@levu推荐一种将PHP变量的值转换为JavaScript的好方法。

#2


8  

<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">

#3


3  

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.

我希望这能对你有所帮助。

#4


2  

Encode as JSON before outputting.

在输出之前编码为JSON。

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

#5


1  

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中的字符串来获取数组。

#6


1  

<?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.

嘿,请使用上面的你想要的结果。这是经过测试的代码。

#7


0  

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>';