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.

5
  • average = <?php echo $average; ?>; Commented May 27, 2016 at 11:08
  • You can not put php code into js file Commented May 27, 2016 at 11:08
  • 1
    Is this in a php file or a javascript file? Note that the server will not parse a javascript file as php. Commented May 27, 2016 at 11:10
  • Possible duplicate of How to pass variables and data from PHP to JavaScript? Commented May 27, 2016 at 11:33
  • I am trying to add the code to a html file but using the <script> tag to use javascript Commented May 27, 2016 at 11:45

6 Answers 6

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');
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

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 <script> tag to write code in javascript
You can either put all in the .php file then or do as Viktor Koncsek suggested.
1

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

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

Comments

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.

8 Comments

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.
@LukeRayner did you trigger the ajax call the moment you needed it? Like in a $(function(){ }); block on e.g. a button click event?
Why cant i just call the return average function because all i want is the return value of $average
I need it all the time not on an action
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 <script> tags to call heatmap.js and display on the page a heatmap. But for me to do this i need to call some variables i set in my DBObject.php file and use them in my index.html file.
|
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.

Comments

0

Try this:

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

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.