0

I have been reading so many posts and none of them seem to work for me I have no idea what I am doing wrong. I want to call a PHP integer in JavaScript and set it to a JavaScript variable so I am then able to use that variable in my calculations.

average = '<?php echo $average; ?>';
console.log("value" + " " + average);

This is the code I have been using and it prints to the console:

value <?php echo $average; ?>

This is clearly not the resulted I wanted, I wanted it to print the integer. My $average PHP integer is also located in a different file so I used:

<?php include 'DBObject.php';?>

I want to be able to do a calculation like:

drone1percentage = 1000 / average * 100;

but since the average variable isn't taking the php integer $average from my other file it doesn't work.

Luke Rayner
  • 391
  • 6
  • 20

6 Answers6

1

You could add this to your .htaccess, so PHP will interpret js files too.

<FilesMatch "\.(js)$">
AddHandler application/x-httpd-php .js
</FilesMatch>

Or rename that .js to .php and add this header in front of the file:

header('Content-Type: application/javascript');
Viktor Koncsek
  • 564
  • 3
  • 13
1

You "cannot" execute php inside a javascript file.

However there are some tricks to get to your variables from php.

One of them is creating a javascript variable with php and use this variable inside your javascript file.

Example:

// Inside your php file

<script>

    var average = <?php echo $average; ?>

</script>

<script src="yourjavascriptfile.js"></script>

// Inside your javascript file

console.log(average);
Thomas Van der Veen
  • 3,136
  • 2
  • 21
  • 36
  • do I use the code i used in my question to call that php file? Will this work in a html file I am just using the – Luke Rayner May 27 '16 at 11:56
  • You can either put all in the .php file then or do as Viktor Koncsek suggested. – Adder May 27 '16 at 12:12
1

use jQuery.ajax - http://api.jquery.com/jquery.ajax/

var average;
$(function(){
    $.get("path/to/script", function(data, status){
        average = data;
    });
});
vocipuc
  • 11
  • 1
0

Your js file has to end in .php for example example.js.php in order for the php code to be parsed.

While you cannot put php code into a .js file, you can create a js file from php

You could also setup the web server to parse all .js files as php code, but that would be a bit too much perhaps.

Adder
  • 5,708
  • 1
  • 28
  • 56
0

Try this:

average = parseInt('<?php echo $average; ?>');

István
  • 5,057
  • 10
  • 38
  • 67
0

For me the most reliable way to get information from PHP into javaScript is using jQuerys ajax functionality.

So if you have something like

yourPHPFile.php

<?php
if($_POST['action'] == 'getAverage') {
    echo returnAverage();
    die;
}

function returnAverage() {
    //your average calculation here

    return $average
}

You could do something like

yourJavaScriptFile.js

var average = null;

$.ajax({
    method: 'POST',
    url: '/path/to/yourPHPFile.php',
    data: {action: 'getAverage'},
    success: function(response) {
        average = response;
    }
});

Just put this inside a function that is triggered whenever you need it.

For this to work you need to have jQuery included. For further information about the jQuery.ajax() functionality see The Documentation.

Please also note that some ajax parameters might differ depending on the version of jQuery you use.

Mr.Moe
  • 507
  • 1
  • 5
  • 19
  • I tried this and it isnt passing the php value of $average to my js variable average, I it just saying the value of average is null because thats what we set it above. – Luke Rayner May 27 '16 at 12:13
  • @LukeRayner did you trigger the ajax call the moment you needed it? Like in a $(function(){ }); block on e.g. a button click event? – Mr.Moe May 27 '16 at 12:15
  • Why cant i just call the return average function because all i want is the return value of $average – Luke Rayner May 27 '16 at 12:17
  • I need it all the time not on an action – Luke Rayner May 27 '16 at 12:17
  • Basically what im doing is creating a heatmap to show how busy it is in an area, I do this by getting data from a data base then use that data to work out the average (I do this in PHP). Then in my index.html file i am using the – Luke Rayner May 27 '16 at 12:23
  • @LukeRayner alright. so what I would do is $(function() { //the ajax call here }); In your index.html files ` – Mr.Moe May 27 '16 at 12:27
  • It is still returning the value as null. Do I basically copy your code exactly and change the url and the comment in the returnAverage function because that is what I have done. – Luke Rayner May 27 '16 at 12:44
  • I uploaded my project to github could you please have a look at my code, Thank you for your help btw https://github.com/Lukerayn3r/HeatmapProject – Luke Rayner May 27 '16 at 12:58