PHP與javascript的兩種交互方式
更新時(shí)間:2006年10月09日 00:00:00 作者:
在網(wǎng)頁(yè)制作過程中怎樣在不刷新頁(yè)面的情況下使前臺(tái)頁(yè)面和
后臺(tái)CGI頁(yè)面保持交互一直是個(gè)問題。這里介紹兩個(gè)我在實(shí)踐中使
用的方法。
方法一:通過Cookie交互。一共是三個(gè)文件,分別為:
index.htm,action.php,main.htm
原理為前臺(tái)頁(yè)面main.htm和后臺(tái)action.php通過頁(yè)面框架
index.htm組織起來,將action.php的頁(yè)面寬度設(shè)為0,這樣并不
影響顯示。action.php將信息放入cookie中,main.htm通過讀取
cookie來實(shí)現(xiàn)交互。在main.htm中也可以通過重新讀取action.php
來實(shí)現(xiàn)控制后臺(tái)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>本頁(yè)使用頁(yè)面框架,但是您的瀏覽器不支持。</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" >
當(dāng)前參數(shù)為<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.*.*來實(shí)現(xiàn)交互。一共是三個(gè)文件,分別為:
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" >
當(dāng)前參數(shù)為<input type="text" name="current_cookie" size="80" maxlength="1000">
</form>
<br>
<a href="action.php" target="leftFrame">重新讀取Cookie</a>
</body>
</html>
---------------------------------------------------------------
后臺(tái)CGI頁(yè)面保持交互一直是個(gè)問題。這里介紹兩個(gè)我在實(shí)踐中使
用的方法。
方法一:通過Cookie交互。一共是三個(gè)文件,分別為:
index.htm,action.php,main.htm
原理為前臺(tái)頁(yè)面main.htm和后臺(tái)action.php通過頁(yè)面框架
index.htm組織起來,將action.php的頁(yè)面寬度設(shè)為0,這樣并不
影響顯示。action.php將信息放入cookie中,main.htm通過讀取
cookie來實(shí)現(xiàn)交互。在main.htm中也可以通過重新讀取action.php
來實(shí)現(xiàn)控制后臺(tái)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>本頁(yè)使用頁(yè)面框架,但是您的瀏覽器不支持。</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" >
當(dāng)前參數(shù)為<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.*.*來實(shí)現(xiàn)交互。一共是三個(gè)文件,分別為:
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" >
當(dāng)前參數(shù)為<input type="text" name="current_cookie" size="80" maxlength="1000">
</form>
<br>
<a href="action.php" target="leftFrame">重新讀取Cookie</a>
</body>
</html>
---------------------------------------------------------------
相關(guān)文章
Linux下PHP+MYSQL+APACHE配置過程 (摘)
Linux下PHP+MYSQL+APACHE配置過程 (摘)...2006-12-12PHP函數(shù)之日期時(shí)間函數(shù)date()使用詳解
date()函數(shù)是我們?cè)趐hp開發(fā)中常碰到并且會(huì)使用到的一個(gè)日期函數(shù),下面我來給大家介紹date()函數(shù)的一些基本扮靚和方法,有需要了解的朋友可進(jìn)入?yún)⒖?/div> 2013-09-09最新評(píng)論