Daniel C
Daniel C

Reputation: 627

Printing PHP to Javascript

I have the following php script which returns a string of numbers with commas separating them:

<?php include "printqueryrows.php"; ?>

returns

 2.00 , 1.00 , 21.16 , 6.75 , 6.00 , 6.25 , 3.00 , 

I have a javascript chart creator that requires the data in the following format:

<script type="text/javascript"> ...
                {
                name: 'London',
                data: [2.00 , 1.00 , 21.16 , 6.75 , 6.00 , 6.25 , 3.00 , ]
              }
</script>

In order to include the results from the php file in the data field (so that it prints out the numbers, separated by commas, I use this script:

<script type="text/javascript"> ...
    data: [<?php include "printqueryrows.php"; ?>]
</script>

However, this is not printing the data. If I do this line individually in another script, the data prints. But it doesn't print when I try to print it into this javascript file. Any suggestions? Thanks!

Upvotes: 0

Views: 180

Answers (2)

janenz00
janenz00

Reputation: 3310

Check two things :

  1. Make sure that JS is embedded in a file with .php extension

  2. Make sure that your printqueryrows.php does :

    echo "2.00 , 1.00 , 21.16 , 6.75 , 6.00 , 6.25 , 3.00 , ";

Upvotes: 0

Peter Sobot
Peter Sobot

Reputation: 2557

You need to ensure that the file that contains your Javascript is itself a PHP script - make sure it ends in .php. If you're not sure if it's worked, check the source of the resulting web page. It should not contain <?php anywhere in the browser-side source.

Upvotes: 1

Related Questions