php后台程序与Javascript的两种交互方式

yipeiwu_com5年前PHP代码库
方法一:通过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
复制代码 代码如下:

<?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>

相关文章

php生成xml简单实例代码

当处理基于XML应用程序时,开发者经常需要建立XML编码数据结构。例如,Web中基于用户输入的XML状态模板,服务器请求XML语句,以及基于运行时间参数的客户响应。 尽管XML数据结构的...

php 文本文件的读取效率

首页大概3KB,是在本地测试的 复制代码 代码如下: file_get_contents('shadow.xml'); 耗时 0.0003 秒 复制代码 代码如下: $indexFil...

PHP 5.0创建图形的实用方法完整篇第1/3页

PHP 5.0创建图形的实用方法完整篇第1/3页

本文将展示如何使用 PHP 构建面向对象的图形层。使用面向对象的系统可以用来构建复杂的图形,这比使用标准 PHP 库中所提供的基本功能来构建图形简单很多。   我将图形编辑程序分为两类:...

php 获取页面中指定内容的实现类

功能: 1.获取内容中的url,email,image。 2.替换内容中的url,email,image。 url:<a href="url">xxx</a> e...

PHP 文件缓存的性能测试

PHP常用缓存方式:第一种,把需要缓存的数据进行处理,形成PHP可以直接执行的文件。在需要缓存数据的时候,通过include方式引入,并使用。第二种,把需要的数据通过serialize函...