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

yipeiwu_com4年前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>

相关文章

CodeIgniter上传图片成功的全部过程分享

最近几天正在做一个小型CMS,用到图片上传了,想利于CodeIgniter的上传类去实现,但测试中有好多问题,我把经过和要注意的地方分享一下!复制代码 代码如下:<?php ech...

PHP实现广度优先搜索算法(BFS,Broad First Search)详解

本文实例讲述了PHP实现广度优先搜索算法。分享给大家供大家参考,具体如下: 广度优先搜索的算法思想 Breadth-FirstTraversal 广度优先遍历是连通图的一种遍历策略。因为...

WordPress中限制非管理员用户在文章后只能评论一次

之前有网友提出,在WordPress中有没有办法实现每篇文章只允许用户评论一次? 暂不说这个需求有没有用,毕竟WordPress就是给有各种需求的人用的。这个功能实现起来也比较简单,只需...

php实现根据字符串生成对应数组的方法

本文实例讲述了php实现根据字符串生成对应数组的方法,是比较实用的技巧。分享给大家供大家参考。具体方法如下: 先看看如下示例: <?php $config = arr...

PHP伪造referer实例代码

这里就直接给出完整的程序吧,具体的应用可以自己修改。 我这里给出的例子是很简单的,其实可以从这个例子中发展出很多的应用。比如隐藏真实的URL地址……嘿嘿,具体的就自己分析去吧 这里新建一...