0

I am using a jquery flot map and i want to add a php variable to generate the values, I thinki have done something like this before but just cannot get it to work. I have two pages the first is the stats page which calls the chart as a class and displays the information from the flot.js page.

Below is a snippet of the code where it shows how the variables are put together.

var d1 = [];
for (var i = 1; i <= 14; i += 1)
d1.push([i, parseInt(Math.random() * 45000)]);

what i want to do is add a variable to the page something like this,

 var d1 = [];
for (var i = 1; i <= 14; i += 1)
d1.push([i, parseInt(Math.random() * <?php echo $test ?>)]);

with the value of the test var being set in the stats page before the flot.js page is called if that all makes sense.

At the moment the graph works fine in it's original state but as soon as I add a php var to the page it displays nothing, any advice would be appreciated.

3
  • What is the extension of the file where the second part of the js is located, is it a file-type that will be parsed as php by the server? Commented Sep 29, 2012 at 2:24
  • Are you sure that $test is being defined before it's being output via the javascript code? assuming php is enabled on the webserver, that should work fine. Commented Sep 29, 2012 at 2:25
  • Your .js file will need to be a .php file to allow PHP code inside of it. Setting one variable in your "stats page" won't automatically cause PHP to execute your JS file as PHP code on the server - you need to explicitly run it as such. Commented Sep 29, 2012 at 2:28

3 Answers 3

1

If you want to use php in a js page, you need to tell the server to treat .js files as .php files.

You could do that for example in an .htaccess file like:

AddType application/x-httpd-php .js
AddHandler x-httpd-php5 .js

<FilesMatch "\.js$">
SetHandler application/x-httpd-php
</FilesMatch>

That's what I use for html files on some sites anyway.

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

Comments

0

I would not do that on the server. If all the js go through php brr....

Just use for this specifi case

<script src="url/file.js.php"></script>

However this will slowdown the page load.

1 Comment

Thanks but it's still just loading a blank graph with no data
0

Solved, I just copied the content of the javascript page to the chart page and the variable worked.

Thanks

Everybody

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.