[英]Pass php var to Javascript from an ajax call
I would create an ajax code that load a php page every x seconds.我会创建一个 ajax 代码,每 x 秒加载一个 php 页面。
Result: obtain a specific php var in the loaded page.结果:在加载的页面中获取特定的 php var。
Then: assign the value of the obtained var to a javascript.然后:将获得的 var 的值赋给一个 javascript。
Polling, in your case downloading a page every x seconds, is hardly ever the best approach to a problem.轮询,在您的情况下每 x 秒下载一个页面,几乎不是解决问题的最佳方法。 It wastes resources.
它浪费资源。
However, if you want to do something with Ajax, I suggest you use JQuery:但是,如果你想用 Ajax 做点什么,我建议你使用 JQuery:
https://api.jquery.com/jquery.get https://api.jquery.com/jquery.get
An example, suppose you have a php 'test.php' file with:一个例子,假设你有一个 php 'test.php' 文件:
echo $myVariable;
You can then do this in javascript:然后,您可以在 javascript 中执行此操作:
$.get("test.php", function( data ) {
alert( "The PHP variable is: "+data);
});
Combine this with a Javascript timer and you're done.将此与 Javascript 计时器相结合,您就完成了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.