欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【PHP与Javascript的两种交互方式】,下面是详细的分享!
PHP与Javascript的两种交互方式
在网页制作过程中怎样在不刷新页面的情况下使前台页面和
后台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=http://www.chinaz.com/program/2006/1116/"action.php">
<frame name="rightFrame" scrolling="auto" src=http://www.chinaz.com/program/2006/1116/"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=http://www.chinaz.com/program/2006/1116/"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=http://www.chinaz.com/program/2006/1116/"action.php" target="leftFrame">重新读取Cookie</a>
</body>
</html>
---------------------------------------------------------------
以上所分享的是关于PHP与Javascript的两种交互方式,下面是编辑为你推荐的有价值的用户互动:
相关问题:js与php参数交互
答:你需要的应该是xhr,你可以使用ajax,或者简化的$.post(),$.get()等, 我不知道你的url和php的处理函数、传递值的方式,我自定义了 url=test.php php处理函数在test.php文件中, 方式是get $.get('test.php?str=str', function(data){ alert(da... >>详细
相关问题:javascript与php交互。并存入数据库
答:只能是ajax提交php读数据库, 然后ajax获取返回值,用js进行处理,然后将处理结果在传给php处理,。 思路的例子是这样:可能有错误,没测试 php:data_read.php html: js: function ajax_test(params){//params='&act=read&a=selectname....' $... >>详细
相关问题:php 的用户交互问题
答:1、在注册的时候,先按输入的用户名,查询一下数据库对应字段,是否存在记录。存在则提示错误,不存在则继续注册,一般用php就可独立完成。 2、在注册的时候,当输入结束时,通过javascript实现ajax,以已输入的用户名作为索引,实时查询数据库... >>详细
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
