Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm trying to pass a php variable to a javascript variable(on different files).

myPHP.php

<?php
include'myJS.php';
$hello="Hello";
echo json_encode($hello);  

myJS.php

<html>
<head>
    <title>Hello</title>
    <script>
        var data;
        var oReq = new XMLHttpRequest();
        oReq.onload = function () {
            data = this.responseText;

            alert(data);
        };
        oReq.open("get", "myPHP.php", true);
        oReq.send();
    </script>
</head>
</html>

I'm running the myPHP.php file (and not myJS.php). I'm not getting any error, however, in the variable 'data' in myJS.php file, instead of storing the value of only the PHP variable, the entire myJS.php file is getting stored. How can I avoid this and store just the variable value?

P.S. This is a sample code, I would be implementing this logic with dynamic data, so please do suggest me solutions which can work with dynamic data.

share|improve this question
    
Make sure that myJS.php does not output anything. That includes html, text, javascript, etc. – jeroen Apr 7 '15 at 11:18
    
How can I check if the variable value is being stored in the JS variable or not? – user2195963 Apr 7 '15 at 11:27
    
I'm not sure what you mean, why do you include a js file? You don't seem to need it. – jeroen Apr 7 '15 at 11:28
    
I have some data coming into my PHP page, which I need to store into a Javascript variable. So, in real time, I would be calling the PHP page first which would then navigate to the myJS.php file so that it can be stored in the JS variable. – user2195963 Apr 7 '15 at 11:31
    
That doesn't make any sense, the output of your php script is returned and available in javascript in responseText. There - in the onload function - you can store it in a javascript variable. – jeroen Apr 7 '15 at 11:35
up vote 2 down vote accepted

You don't need ajax for static data, just generate javascript:

<?php
$hello="Hello";
?>

<html>
<head>
    <title>Hello</title>
    <script>
        var data = "<?php print $hello; ?>";
    </script>
</head>
</html>

If the data is not static, and you want to use ajax, separate the files:

myJS.php:

<html>
<head>
    <title>Hello</title>
    <script>
        var data;
        var oReq = new XMLHttpRequest();
        oReq.onload = function () {
            data = this.responseText;
            alert(data);
        };
        oReq.open("get", "myPHP.php?ajax=1", true);
        oReq.send();
    </script>
</head>
</html>

Here is myPHP.php:

<?php
if(!isset($_GET['ajax']))
{
    include('myJS.php');
}
else
{
    $hello="Hello";
    echo json_encode($hello);  
}
share|improve this answer
    
I tried the second piece of code, and it made no difference, i.e., I can still see the entire form in the alert instead of just the variable value. – user2195963 Apr 7 '15 at 11:24
    
@user2195963 You did something wrong. You must call myJS.html, and remove include from myPHP.php – user4035 Apr 7 '15 at 11:28
    
My requirement is to call the PHP file and not the HTML file. Is there a way to do so? – user2195963 Apr 7 '15 at 11:29
    
@user2195963 Yes, but you'll need to pass a variable to distinguish between ajax and non-ajax calls. Look at the updated answer. – user4035 Apr 7 '15 at 11:33
    
Its working now, thank you :) – user2195963 Apr 7 '15 at 11:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.