PHP与Javascript的两种交互方式
来源:互联网 发布:图像形态学 知乎 编辑:程序博客网 时间:2024/05/16 04:47
在网页制作过程中怎样在不刷新页面的情况下使前台页面和
后台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
---------------------------------------------------------------
<?
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>
---------------------------------------------------------------
后台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
---------------------------------------------------------------
<?
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与Javascript的两种交互方式
- PHP与Javascript的两种交互方式
- PHP与Javascript的两种交互方式
- PHP与Javascript的两种交互方式
- PHP与Javascript的两种交互方式
- 服务器脚本与Javascript的两种交互方式(以php为例)
- javascript中与php交互的方式
- Flex(ActionScript)与JavaScript交互的两种方式示例
- Flex(ActionScript)与JavaScript交互的两种方式示例
- Qt和Javascript交互的两种方式
- Flex两种与后台JAVA交互的方式
- Flex两种与后台JAVA交互的方式
- Flex两种与后台JAVA交互的方式 .
- Unity3D与iOS交互的两种方式
- android与服务端交互的两种方式
- Android--实现H5与Native交互的两种方式
- mybatis与数据库的两种交互方式
- C++与QML实现交互的两种基本方式
- 视频网站中的推荐系统
- .net 查找性能瓶颈的利器 ANTS Performance Profiler 使用方法及破解
- OCP-1Z0-053-V12.02-390题
- Ruby for Rails 最佳实践十六
- 最大堆及堆排序的Java实现_world
- PHP与Javascript的两种交互方式
- 搭建eclipse开发quick-cocos2d-x开发环境(转)
- linux dup与dup2
- html中iframe子页面与父页面元素的访问以及js变量的访问
- Html+CSS浮动的广告条
- 详解Android动画之Tween Animation
- 【解决方案】c3p0 APPARENT DEADLOCK创建紧急线程
- CentOS安装squid代理服务器
- linux使用git对github项目管理