-2

My overall goal was to pass an array of numbers from php to js. I saw solutions elsewhere to just put within the javascript and so I have been trying that. However, I keep getting an error "Unexpected Token ?" when I add in the line with php. I have checked that $ztable is in fact the variable I want and that it does properly exist. Below are three different examples of what I have tried and all of them returned the same error. I have tried a couple other methods of getting the variable from php to the javascript as well but none seemed to worked right for me. If there is something wrong with the code I have written please let me know, or if there is another simple method of transferring the variable I would love to hear it. With my application the variable is only being passed once so speed is not an issue, nor is security (for different reasons).

<script>
ztable = new array(<?php echo json_encode($ztable); ?>);
....other unrelated code.....
</script>

<script>
var ztable = <?php echo json_encode($ztable); ?>;
....other unrelated code.....
</script>

<script>
var ztable = <?php echo $ztable; ?>;
....other unrelated code.....
</script>

edit: I am not using jQuery, and my php code is pretty simple, it just generates an array of numbers based on the users input along the lines of

$ztable = [0.001, 0.003, 0.006, 0.01, 0.02, 0.04, 0.07, 0.11, 0.16, 0.23, 0.31, 0.4, 0.5, 0.4, 0.31, 0.23, 0.16, 0.11, 0.07, 0.04, 0.02, 0.01, 0.006, 0.003, 0.001];

Jicnon
  • 3
  • 3

2 Answers2

1

I think you are looking for something like:

<script type="application/javascript">
    var ztable = JSON.parse(<?php echo json_encode(isset($ztable) ? $ztable : array()); ?>);
    //.... more stuff
</script>

Your examples do not seem to convert the json from a string in the javascript.

This also provides a default empty array if $ztable is not defined, which will fail gracefully.

mopsyd
  • 1,877
  • 3
  • 20
  • 30
  • When I try to use the line of code you provided I still get an error "Unexpected Token ?" which just makes it get an array of null all the time, even when I know $ztable is set. But assuming I can get around that error this line of code does seem like it could be very useful. – Jicnon May 05 '16 at 19:44
  • Place `' . print_r($ztable, 1) . ''; die(); ?>` right before your javascript tag to check the output immediately before render. If you are having strange variable problems, always check output immediately before it is called to rule out some other influence. If you are passing by reference elsewhere in your code, this may also cause issues that are hard to debug. – mopsyd May 28 '16 at 21:24
0

The 2nd examples works perfectly fine. This works and I think this is what you might want to do.

<script>
var ztable = <?php echo json_encode($ztable); ?>;
....other unrelated code.....
</script>

Potential issue here might be is that the function json_encode is not defined. I suggest to check this first.

The other thing to do here is to check output from PHP and if still does not work show it in your question.

The 3rd example looks broken

<script>
var ztable = <?php echo $ztable; ?>;
....other unrelated code.....
</script>

$ztable is an array. How do you expect to echo it? This brings a warning and result looks like

<script>
    var ztable = PHP Notice:  Array to string conversion in /home/victor/Projects/emailmarketing/rank/code.php on line 14
Array;
</script>

Even if warning is not shown the result does not look like a valid JS code.

Community
  • 1
  • 1
Victor Smirnov
  • 3,450
  • 4
  • 30
  • 49