怎样把php的变量传到javascript里面去 用php直接去写javascript就行了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 怎样从 Javascript 传递一个变量到 PHP? 由于 Javascript (通常情况下)是客户端技术,而 PHP (通常情况下)是服务器端技术,而且 HTTP 是一种“无状态”协议,因此两种语言之间不能直接共享变量。 但是,有可能在二者之间传递变量。一种实现的方法是用 PHP 生成 Javascript 代码,并让浏览器自动刷新,将特定的变量传递回 PHP 脚本。以下例子显示了如何这样做 -- 让 PHP 代码取得显示屏幕的高度和宽度,通常只能在客户端这么做。 <?phpif (isset($_GET['width']) AND isset($_GET['height'])) { // output the geometry variables echo "Screen width is: ". $_GET['width'] ."<br />\n"; echo "Screen height is: ". $_GET['height'] ."<br />\n";} else { // pass the geometry variables // (preserve the original query string // -- post variables will need to handled differently) echo "<script language='javascript'>\n"; echo " location.href=\"${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}" . "&width=\" + screen.width + \"&height=\" + screen.height;\n"; echo "</script>\n"; exit();}?> 就是用Php写js的办法比如定义了var a;那么 用php echo "a=".xxx; <?$msg = "简单的测试~";?><script language="javascript">alert("<? echo $msg;?>");</script> 将javascript变量传递到PHP脚本中的方法方法一: 先用PHP生成js代码,然后通过浏览器的自动刷新,将javascript变量传递到PHP脚本中。<?phpif ($qq){ echo $my;}else{ echo "<script>var my='我是xxx<br>';location.href='?&my='+my;</script>"; exit();}// 注意:在实际应用中要防止用户在地址栏内更改传递的变量值$web="我就是xxx!".$my;echo $web;?>方法二: 先用document.cookie方法把js变量写入cookie中,再用PHP取出。<script>var name="my",value="xxx";document.cookie=name+"="+value+";"</script><?phpecho "$my\n";$myname="我是".$my;echo $myname;?> 我是想做一个文字特效:代码如下<!-- Beginfunction initArray() {this.length = initArray.arguments.length;for (var i = 0; i < this.length; i++) {this[i] = initArray.arguments[i]; }}var ctext = "当前已有$i_station_count人访问贵网站";var speed = 2000;var x = 0;var color = new initArray("#ffff00", "#ff0000", "#ff00ff","#0000ff","#ffffff", "#000000", "#00ff00","#00ffff","#ff0ff0");if(navigator.appName == "Netscape") {document.write('<layer id="c"><center>'+ctext+'</center></layer><br>');}if (navigator.appVersion.indexOf("MSIE") != -1){document.write('<div id="c"><center><b>'+ctext+'</b></center></div>');}function chcolor(){ if(navigator.appName == "Netscape") {document.c.document.write('<center><font color="'+color[x]);document.c.document.write('">'+ctext+'</font></center>');document.c.document.close();}else if (navigator.appVersion.indexOf("MSIE") != -1){document.all.c.style.color = color[x];}(x < color.length-1) ? x++ : x = 0;}setInterval("chcolor()",1000);//alert("<? echo $i_station_count;?>");// End -->而$i_station_count是php的变量,请问要怎么该后它可以以js的形式在php上运行 var ctext = "当前已有$i_station_count人访问贵网站";$i_station_count是php 的变量,这句怎么写 用PHP直接定不就可以了嘛,比如echo'<script language="javascript">{window.location(index.php?id=3);}</script>'; to skystar008(疯花血月): 这个我知道,但是我想把服务器端的值是从php查询数据库里的值传到客户端,也就是javascript 的变量.能达到吗? 怎么用PHP程序控制IIS的创建和修改相应参数 php能调用C#的exe程序吗,,如果能,怎么在网页中显示出来呢 关于php邮件的问题 对于任意的数据类型,如果得到一个签名? 谁有生成静态页面的代码?能给一个吗?? --如何把多个文件添加至一个压缩包,并能正常下载????? 生成静态页面的各种方法 关于类的一些问题 在php中,如何实现发送邮件?在线等候! PHP 传值给 C#窗体 在线求助,关于PHP的变量的问题 當鏈接頁資料還沒處理完時,先顯示“正在loading..........”的畫面
if (isset($_GET['width']) AND isset($_GET['height'])) {
// output the geometry variables
echo "Screen width is: ". $_GET['width'] ."<br />\n";
echo "Screen height is: ". $_GET['height'] ."<br />\n";
} else {
// pass the geometry variables
// (preserve the original query string
// -- post variables will need to handled differently) echo "<script language='javascript'>\n";
echo " location.href=\"${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}"
. "&width=\" + screen.width + \"&height=\" + screen.height;\n";
echo "</script>\n";
exit();
}
?>
比如定义了var a;那么 用php echo "a=".xxx;
$msg = "简单的测试~";
?>
<script language="javascript">
alert("<? echo $msg;?>");
</script>
先用PHP生成js代码,然后通过浏览器的自动刷新,将javascript变量传递到PHP脚本中。<?php
if ($qq)
{
echo $my;
}
else
{
echo "<script>var my='我是xxx<br>';location.href='?&my='+my;</script>";
exit();
}
// 注意:在实际应用中要防止用户在地址栏内更改传递的变量值$web="我就是xxx!".$my;
echo $web;
?>方法二:
先用document.cookie方法把js变量写入cookie中,再用PHP取出。<script>
var name="my",value="xxx";
document.cookie=name+"="+value+";"
</script>
<?php
echo "$my\n";
$myname="我是".$my;
echo $myname;
?>
代码如下
<!-- Begin
function initArray() {
this.length = initArray.arguments.length;
for (var i = 0; i < this.length; i++) {
this[i] = initArray.arguments[i];
}
}
var ctext = "当前已有$i_station_count人访问贵网站";
var speed = 2000;
var x = 0;
var color = new initArray(
"#ffff00",
"#ff0000",
"#ff00ff",
"#0000ff",
"#ffffff",
"#000000",
"#00ff00",
"#00ffff",
"#ff0ff0"
);
if(navigator.appName == "Netscape") {
document.write('<layer id="c"><center>'+ctext+'</center></layer><br>');
}
if (navigator.appVersion.indexOf("MSIE") != -1){
document.write('<div id="c"><center><b>'+ctext+'</b></center></div>');
}
function chcolor(){
if(navigator.appName == "Netscape") {
document.c.document.write('<center><font color="'+color[x]);
document.c.document.write('">'+ctext+'</font></center>');
document.c.document.close();
}
else if (navigator.appVersion.indexOf("MSIE") != -1){
document.all.c.style.color = color[x];
}
(x < color.length-1) ? x++ : x = 0;
}
setInterval("chcolor()",1000);//alert("<? echo $i_station_count;?>");// End -->
而$i_station_count是php的变量,请问要怎么该后它可以以js的形式在php上运行
$i_station_count是php 的变量,这句怎么写
}</script>';
这个我知道,但是我想把服务器端的值是从php查询数据库里的值传到客户端,也就是javascript 的变量.能达到吗?