0

I have my file hotlaps.php where I have created a script of javascript with:

echo "<body onload=\"func1(".$array_1.",".$array_2.",".$array_3.");\">";

In my file hotlaps.js I have that function:

function func1(array1, array2, array3){

    arr1 = array1;
    arr2 = array2;
    arr3 = array3;
    alert(piloto_array[0]);
    start();
}

But my variables arr1, arr2 and arr3 are undefined. Why?

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
LigaVirtual F1
  • 77
  • 1
  • 3
  • 6

4 Answers4

1

Try this:

<body onload='func1(".json_encode($array_1).",".json_encode($array_2).",".json_encode($array_3).");'>";
Marek Kus
  • 150
  • 6
1

if you want to make use of your php arrays inside your javascript variables, then you will need to use json_encode but be wary that it uses double quotes " for arrays, which will break in your example with nested double quotes inside other nested double quotes. use single quotes to avoid that.

I have spaced things out a little better so that you can see what's happening easier.

<?php
$array_1 = array ('one','two','three');
$array_2 = array ('four','five','six');
$array_3 = array ('seven','eight','nine');

echo "
  <body onload='
    func1(
      ".json_encode($array_1).",
      ".json_encode($array_2).",
      ".json_encode($array_3)."
    );
  '/>
";

?>

<script>
function func1(array1, array2, array3){
    arr1 = array1;
    arr2 = array2;
    arr3 = array3;
    alert(arr1);
}
</script>

Results in this output:

  <body onload='
    func1(
      ["one","two","three"],
      ["four","five","six"],
      ["seven","eight","nine"]
    );
  '/>

<script>
function func1(array1, array2, array3){
    arr1 = array1;
    arr2 = array2;
    arr3 = array3;
    alert(arr1); // just print the first one, you get the idea
}
</script>
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
  • You may need to escape the single quotes. After all, this is quite messy. You're better off not using `

    ` but e.g. jQuery's `$(document).ready`

    – ssc-hrep3 Jun 13 '16 at 22:49
  • @ssc-hrep3 I agree with not using any of the `on*` [obtrusive javascript attributes](https://en.wikipedia.org/wiki/Unobtrusive_JavaScript), but that was per the OP example, as well as the terrible `alert`. As for nested quotes issues, it does depend on the data whether additional escaping is necessary, but double can be guaranteed to break. I have expanded the whitespace on my example to better demonstrate. – Jeff Puckett Jun 13 '16 at 22:57
  • Thanks!!! :DDD It works – LigaVirtual F1 Jun 13 '16 at 22:58
  • @LigaVirtualF1 glad it works for you :) feel free to accept if it answers the question for you. this will result in +2 rep for you. see: [How does accepting an answer work?](http://meta.stackexchange.com/q/5234/321521) – Jeff Puckett Jun 13 '16 at 23:17
1

To simplify the syntax and avoid the mess with the quotes (IMHO) you could initialize the variables in PHP and JS like that:

<?php
$array_1 = json_encode([1,2,3]);
$array_2 = json_encode([4,5,6]);
$array_3 = json_encode([7,8,9]);
?>
<script type="text/javascript">
    var array_1 = <?php echo $array_1; ?>,
        array_2 = <?php echo $array_2; ?>,
        array_3 = <?php echo $array_3; ?>;
</script>
<body onload="func1(array_1,array_2,array_3)">
<script type="text/javascript">
    function func1(array1, array2, array3) {
        console.log(array1);
    }
</script>
postrel
  • 134
  • 8
-1

You need to print the variables from php into your html code

<body onload=\"func1("<?php echo $array_1; ?>","<?php echo $array_2; ?>","<?php echo $array_3; ?>");\">";