PHP与Javascript的两种交互方式

在网页制作过程中怎样在不刷新页面的情况下使前台页面和
后台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
---------------------------------------------------------------

 1<html>
 2<head>
 3<title>Test</title>
 4<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
 5</head>
 6<frameset border="false" cols="0,*" frameborder="0" framespacing="0">
 7<frame name="leftFrame" noresize="" scrolling="no" src="action.php"/>
 8<frame name="rightFrame" scrolling="auto" src="main.htm"/>
 9</frameset><noframes>
10<body bgcolor="#FFFFFF">
11<p>本页使用页面框架,但是您的浏览器不支持。</p>
12</body>
13</noframes>
14</html>

---------------------------------------------------------------

action.php
---------------------------------------------------------------

---------------------------------------------------------------

main.htm
---------------------------------------------------------------

 1<html>
 2<head>
 3<title>Test</title>
 4<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
 5<script language="javascript">   
 6function get_cookie()   
 7{   
 8document.test.current_cookie.value=document.cookie;   
 9}   
10</script>
11</head>
12<body bgcolor="#FFFFFF">
13<form name="test">   
14当前参数为<input maxlength="1000" name="current_cookie" size="80" type="text"/>
15</form>
16<script language="javascript">   
17setInterval("get_cookie()",200);   
18</script>
19<br/>
20<a href="action.php" target="leftFrame">重新读取Cookie</a>
21</body>
22</html>

---------------------------------------------------------------

方法二:直接通过parent.*.*来实现交互。一共是三个文件,分别为:
index.htm,action.php,main.htm,其中index.htm和前面的一样。
原理为通过parent.rightFrame.test.current_cookie.value直接传递
信息。

action.php
---------------------------------------------------------------

1<script language="javascript">   
2parent.rightFrame.test.current_cookie.value="<? echo $result?>";   
3</script>

---------------------------------------------------------------

main.htm
---------------------------------------------------------------

 1<html>
 2<head>
 3<title>Test</title>
 4<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
 5</head>
 6<body bgcolor="#FFFFFF">
 7<form name="test">   
 8当前参数为<input maxlength="1000" name="current_cookie" size="80" type="text"/>
 9</form>
10<br/>
11<a href="action.php" target="leftFrame">重新读取Cookie</a>
12</body>
13</html>

---------------------------------------------------------------

Published At
Categories with Web编程
Tagged with
comments powered by Disqus