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

9
  • Just read the docs here Commented May 5, 2016 at 4:15
  • Show your php code also Commented May 5, 2016 at 4:15
  • Are you using jquery? Commented May 5, 2016 at 4:17
  • 1
    Use the second option. Third option is wrong output from PHP, and first option, in JS no need to use new when making arrays. Can we see what's inside $ztable? Commented May 5, 2016 at 4:19
  • So how does the generated JS looks like? Commented May 5, 2016 at 4:24

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
Place <?php echo '<pre>' . print_r($ztable, 1) . '</pre>'; 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.
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.

9 Comments

That is actually an issue. I hadn't managed to get the code to run that far because of my "Unexpected Token ?" error. I'll have to keep looking for methods to get the array into JavaScript.
@Jicnon is the file processed by PHP?
yes it is, most of the code is in PHP, I just have a little bit of javascript for a figure.
@Jicnon What is the HTML and JS code you see in browser? What is the exact JS code for the 2nd example which produced the JS error?
The code I have above is the exact JS code that is producing the error. It doesn't like the ? in the <?php.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.