Helio Helio - 4 months ago 6x
Ajax Question

Pass php var to Javascript from an ajax call

I would create an ajax code that load a php page every x seconds.

Result: obtain a specific php var in the loaded page.

Then: assign the value of the obtained var to a javascript.


Polling, in your case downloading a page every x seconds, is hardly ever the best approach to a problem. It wastes resources.

However, if you want to do something with Ajax, I suggest you use JQuery:


An example, suppose you have a php 'test.php' file with:

echo $myVariable;

You can then do this in javascript:

$.get("test.php", function( data ) {
  alert( "The PHP variable is: "+data);

Combine this with a Javascript timer and you're done.