0

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!

4
  • 1
    Correct me if I'm wrong, but you're just putting data: etc without curly brackets? Commented Oct 8, 2012 at 2:21
  • Does the HTML-File with the javascript-code have the correct file extension? (.php) Commented Oct 8, 2012 at 2:22
  • Try setting var queryrows = <?php include "printqueryrows.php"; ?>; in a script tag. Then after put either alert(queryrows); or console.log(queryrows) to check the value is being set correctly Commented Oct 8, 2012 at 2:26
  • 2
    Please be aware that echoing into JavaScript context may make your application vulnerable to XSS attacks if any of the data is user-generated. Commented Oct 8, 2012 at 2:40

2 Answers 2

1

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.

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

1 Comment

Thanks for the tip. I had some extra <?php in there
0

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 , ";

Comments

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.