1

I would like to know how to get get a PHP variable value from another PHP file using jquery. In fact I have to files: test.html and moslem.php . the code of test.html file is as below:

    <html>
    <head>

    </head>
    <body>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            setTimeout( "test()", 1000);
            function test() {
           $.ajax({
            url: 'moslem.php',
            type: 'POST',
            data: ,
            success: function(data) {
                document.write(data);

            }
            });
            setTimeout ( "test()", 1000);
            }
        </script>
    </body>
</html>

And the code of moslem.php file is as below:

    <?php
  $chaine = "hello!";
?>

I would like to know how can I get the value of the variable $chaine using the jquery code above, then what should I put at the line :

data: ,

of that jquery code.

Thanks in advance.

5
  • your moslem.php isn't doing anything. it's assigning a variable and that's it. You have no output, so nothing gets sent back to the client. echo 'hello' is what you need. Commented May 16, 2014 at 16:21
  • @Marc B: I did what you told me but unfortunately it doesn't display anything on the screen, well, in fact what I need is to access to a PHP variable value which exist in another PHP file..I need just to do that no more...Thanks in advance. Commented May 16, 2014 at 16:42
  • You'll need to be a little clearer. What file is the variable in that you want to access? And what's the file you want to access it from? If you're just using PHP you don't need to use AJAX - you can just use include otherfile.php and any variables set in otherfile.php will now be available in the PHP code you called it from. Commented May 16, 2014 at 16:48
  • you don't "access a php variable". ajax cannot do that. it's just an http request. If you want something sent back to your client, you have to do it as output. Commented May 16, 2014 at 16:55
  • @SharkofMirkwood: well, in fact I am trying to create a notification system for the social network web site that I am doing. I asked many people about how to do that and they advised me to use Ajax to execute PHP code periodically (for example every 5 seconds)...then I decided to try to access to variables which exist in a PHP file using Ajax, but as you said it doesn't work. Well, do you have any idea how to do that and how to create a notification system (will you give me just a simple example? We suppose that we have such table, one there is an insertion, then a notification will be). Commented May 19, 2014 at 8:13

3 Answers 3

1

You don't need to change your JQuery to make that happen; you just need to change your PHP a bit. If you set a variable in PHP, JavaScript isn't going to recognise this. If you make your PHP code output the text instead of putting it in a variable, Javascript will see this output and can do whatever you want with it.

Just change $chaine = "hello!"; to echo "hello!";

Edit: I'd just like to add that when you start passing a lot of data through AJAX you should consider using JSON to keep things organised. If you have an array of data you want to pass to JavaScript, for example, just call json_encode on the data and output that, then you can parse it in your JS code.

Bottom line here, though, is that your front-end code won't 'recognise' variables set in PHP, it can only read output from your server-side code.

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

1 Comment

I did what you told me but unfortunately it doesn't display anything on the screen, well, in fact what I need is to access to a PHP variable value which exist in another PHP file..I need just to do that no more...Thanks in advance.
0

Please try with this

<script type="text/javascript">     
 function test() {
    $.ajax({
        url: 'moslem.php',
        type: 'POST',
        success: function(data) {
            document.write(data);
        }
    });     
 }
 $(document).ready(function() {
   setTimeout( test, 1000);    
 });
</script>

No Need to pass the data parameter into ajax. And php file will remain same as before post.

<?php
  $chaine = "hello!"; 
  echo $chaine;
?>

3 Comments

well, there is juts one thing wrong in the code you gave me :) .. you should type this line: setTimeout( test, 1000); before declaring the function test() because I tried that code but it display the expression "hello" just one once no more :) ...However fortunately I succeeded to make the code works :) ... and it displays the word "hello" every one second like what I wanted exactly...finally I found the solution after a long struggle...It was thanks to you mister biswajitGhosh :) ... really thanks a lot my friend for this help...and by the way, you find the right script below.
<script type="text/javascript"> $(document).ready(function() { setTimeout( test, 1000); }); function test() { $.ajax({ url: 'moslem.php', type: 'POST', success: function(data) { document.write(data+'<br>'); } }); setTimeout( test, 1000); } </script>
Really I appreciate your help so much my friend :)
0

Put your code in a document ready handler -

<script type="text/javascript">
$(document).ready(function() {
    setTimeout( "test()", 1000);
    function test() {
        $.ajax({
            url: 'moslem.php',
            type: 'POST',
            data: ,
            success: function(data) {
                document.write(data);
            }
        });
     setTimeout ( "test()", 1000);
    }
});
</script>

And then fix your PHP -

<?php
  $chaine = "hello!"; 
  echo $chaine;
?>

3 Comments

I did what you told me but unfortunately it doesn't display anything on the screen, well, in fact what I need is to access to a PHP variable value which exist in another PHP file..I need just to do that no more...Thanks in advance.
Are you doing this on a web server? Or are you just trying to do this with your local filesystem? Are there any errors in your browser's console?
I am trying to do that with my local filesystem (using local host..exactly Wampserver) and there is no any errors in my browser's console..it doesn't display any thing on the screen.

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.