当前位置: 首页 > 图文教程 > 网络编程 > PHP > php后台程序与Javascript的两种交互方式

PHP
PHP实例教程:Output Control输出函数
memcached和mysql主从环境下PHP开发
基于LAMP架构设计的WEB框架
PHP代码:验证IPV6地址是否合法的正则
PHP环境快读搭建绿色软件包PHPnow
PHP教程:$_SERVER的详细参数整理
php获取url字符串截取路径的文件名和扩展名的函数
在命令行下运行PHP脚本[带参数]的方法
PHP 实用代码收集
PHP 时间转换Unix时间戳代码
关于php fread()使用技巧
PHPMailer 中文使用说明小结
php addslashes和mysql_real_escape_string
php cout<<的一点看法
PHP 变量的定义方法
php学习之 认清变量的作用范围
php 静态变量与自定义常量的使用方法
认识并使用PHP超级全局变量
通过具体程序来理解PHP里面的抽象类
php读取xml实例代码

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 44 ::
收藏到网摘: n/a

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

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