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>

相关文章

使用Xdebug调试和优化PHP程序之[1]

使用Xdebug调试和优化PHP程序之[1]

作者:Haohappy      MSN: haohappy at msn.com Blog: http://blog.csdn.net/hao...

PHP实现的分解质因数操作示例

本文实例讲述了PHP实现的分解质因数操作。分享给大家供大家参考,具体如下: 思路: 如果要计算$num的质数,则至少收集$num以内的质数数组,判断$num是否在质数数组里: 如果否,则...

PHP使用观察者模式处理异常信息的方法详解

本文实例讲述了PHP使用观察者模式处理异常信息的方法。分享给大家供大家参考,具体如下: 异常信息的捕获对编程测试有着重要的意义,这里结合观察者模式,探索如何处理异常信息。 关于观察者模式...

PHP设计模式之简单工厂和工厂模式实例分析

本文实例讲述了PHP设计模式之简单工厂和工厂模式。分享给大家供大家参考,具体如下: 工厂模式是创建型模式的一种,分为简单工厂模式,工厂模式,抽象工厂模式,简单工厂可以称之为工厂模式的一个...

PHP读取XML格式文件的方法总结

本文实例总结了PHP读取XML格式文件的方法。分享给大家供大家参考,具体如下: books.xml文件: <books> <book> <author&...