如何将PHP变量传递给Javascript

如何将PHP变量传递给Javascript

本文介绍了如何将PHP变量传递给Javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2页,一份PHP和一份Javascript.我想将此PHP脚本中的变量传递到一页上:

I have 2 pages, one PHP and one Javascript.I'd like to pass the variable from this PHP script on one page:

$strFind="SELECT * FROM  cometchat_chatrooms_users WHERE userid=$curmemid";
$result=mysql_query($strFind) or die(mysql_error());
$row=mysql_fetch_array($result);
$room=$row['chatroomid'];

在另一页上的该Javascript:

to this Javascript on another page:

var timestamp = 0;
var currentroom = $room;
var heartbeatTimer;
var minHeartbeat = 3000;
var maxHeartbeat = 12000;

我该怎么做?

推荐答案

您实际上无法在任何地方传递"变量.但是只有标量值.坦白地说,您只能传递文本数据.
因此,在使用javascript的情况下,您有2个选项:

You cannot actually "pass" a variable anywhere. But only a scalar value. Frankly, you can pass only text data.
So, in case of javascript you have 2 options:

  1. 要从PHP生成整个js代码及其所有变量.很像您生成HTML的方式.

  1. To generate whole js code from PHP along with all it's variables. Much like to how you generate HTML.

使用AJAX技术从运行在浏览器中的JS请求一些变量.

To request some variable from JS running in a browser, using AJAX technique.

这篇关于如何将PHP变量传递给Javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 02:33