How to pass PHP array parameter to Javascript Func

2020-04-09 17:50发布

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?

7条回答
等我变得足够好
2楼-- · 2020-04-09 18:22

You can also parse an array for JavaScript with json_encode()

$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>';
查看更多
We Are One
3楼-- · 2020-04-09 18:26
<?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.

查看更多
倾城 Initia
4楼-- · 2020-04-09 18:28

Try this

PHP Code

<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

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

Output

7

I hope this will help you.

查看更多
男人必须洒脱
5楼-- · 2020-04-09 18:28

Encode as JSON before outputting.

<input type="submit" value="Test Javascript" onclick="showMovies(<?php echo json_encode($movies); ?>);" />
查看更多
在下西门庆
6楼-- · 2020-04-09 18:30

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

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.

This is how all web server-side scripting works in all languages.

JavaScript simply cannot know what goes in your movies variable unless you stuff it full of values, in JavaScript.

I recommend you to @levu's answer to see a good way to get your PHP variable's values into JavaScript.

查看更多
可以哭但决不认输i
7楼-- · 2020-04-09 18:38
<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 ".

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">
查看更多
登录 后发表回答