简体   繁体   中英

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:

https://api.jquery.com/jquery.get

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM