PHP与Javascript的两种交互方式
2008-02-23 06:23:17来源:互联网 阅读 ()
后台CGI页面保持交互一直是个问题。这里介绍两个我在实践中使
用的方法。
方法一:通过Cookie交互。一共是三个文件,分别为:
index.htm,action.php,main.htm
原理为前台页面main.htm和后台action.php通过页面框架
index.htm组织起来,将action.php的页面宽度设为0,这样并不
影响显示。action.php将信息放入cookie中,main.htm通过读取
cookie来实现交互。在main.htm中也可以通过重新读取action.php
来实现控制后台CGI程序。
index.htm
---------------------------------------------------------------
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<frameset framespacing="0" border="false" frameborder="0" cols="0,*">
<frame name="leftFrame" scrolling="no" noresize src="action.php">
<frame name="rightFrame" scrolling="auto" src="main.htm">
</frameset><noframes>
<body bgcolor="#FFFFFF">
<p>本页使用页面框架,但是您的浏览器不支持。</p>
</body>
</noframes>
</html>
---------------------------------------------------------------
action.php
---------------------------------------------------------------
<?
srand((double)microtime()*1000000);
$result=rand(0,100);
setcookie("action",$result,time() 900,"/");
?>
---------------------------------------------------------------
main.htm
---------------------------------------------------------------
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="javascript">
function get_cookie()
{
document.test.current_cookie.value=document.cookie;
}
</script>
</head>
<body bgcolor="#FFFFFF">
<form name="test" >
当前参数为<input type="text" name="current_cookie" size="80" maxlength="1000">
</form>
<script language="javascript">
setInterval("get_cookie()",200);
</script>
<br>
<a href="action.php" target="leftFrame">重新读取Cookie</a>
</body>
</html>
---------------------------------------------------------------
方法二:直接通过parent.*.*来实现交互。一共是三个文件,分别为:
index.htm,action.php,main.htm,其中index.htm和前面的一样。
原理为通过parent.rightFrame.test.current_cookie.value直接传递
信息。
action.php
---------------------------------------------------------------
<?
srand((double)microtime()*1000000);
$result=rand(0,100);
?>
<script language="javascript">
parent.rightFrame.test.current_cookie.value="<? echo $result?>";
</script>
---------------------------------------------------------------
main.htm
---------------------------------------------------------------
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#FFFFFF">
<form name="test" >
当前参数为<input type="text" name="current_cookie" size="80" maxlength="1000">
</form>
<br>
<a href="action.php" target="leftFrame">重新读取Cookie</a>
</body>
</html>
---------------------------------------------------------------
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash