I have a php file that I gather data on an array and I need to send this data to an external javascript file. I have read that I need to encode data to JSON format and echo the variable stored, then I try to use this variable on the javascript file but I get an error "Can't find variable: jArray" What I do wrong?
file.php
<?php $playerNames_new = array();
for ($i=0; $i<$_POST["playerNo"]; $i++) {
$j = $i+1;
echo "<form name=\"input\" action=\"#\" method=\"POST\">Player Name $j: <input type=\"text\" name=\"playerName[]\" value=\"\"><br/>";
$playerNames_new[$i] = $_REQUEST['playerName'];
}
?>
<script type="text/javascript">var jArray = <?php echo json_encode($playerNames_new); ?>;</script>
<script type="text/javascript" src="script.js"></script>
script.js
$(document).ready(function() {
// global variables
var playerNames = jArray;});