php后臺程序與Javascript的兩種交互方式
更新時間:2009年10月25日 17:44:03 作者:
在網(wǎng)頁制作過程中怎樣在不刷新頁面的情況下使前臺頁面和后臺CGI頁面保持交互一直是個問題。這里介紹兩個方法。
方法一:通過Cookie交互。
一共是三個文件,分別為:index.htm,action.php,main.htm
原理為前臺頁面main.htm和后臺action.php通過頁面框架 index.htm組織起來,將action.php的頁面寬度設(shè)為0,這樣并不影響顯示。action.php將信息放入cookie中,main.htm通過讀取 cookie來實現(xiàn)交互。在main.htm中也可以通過重新讀取action.php 來實現(xiàn)控制后臺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" >
當前參數(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.*.*來實現(xiàn)交互。
一共是三個文件,分別為: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" >
當前參數(shù)為<input type="text" name="current_cookie" size="80" maxlength="1000">
</form>
<br>
<a href="action.php" target="leftFrame">重新讀取Cookie</a>
</body>
</html>
一共是三個文件,分別為:index.htm,action.php,main.htm
原理為前臺頁面main.htm和后臺action.php通過頁面框架 index.htm組織起來,將action.php的頁面寬度設(shè)為0,這樣并不影響顯示。action.php將信息放入cookie中,main.htm通過讀取 cookie來實現(xiàn)交互。在main.htm中也可以通過重新讀取action.php 來實現(xiàn)控制后臺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" >
當前參數(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.*.*來實現(xiàn)交互。
一共是三個文件,分別為: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" >
當前參數(shù)為<input type="text" name="current_cookie" size="80" maxlength="1000">
</form>
<br>
<a href="action.php" target="leftFrame">重新讀取Cookie</a>
</body>
</html>
您可能感興趣的文章:
- js獲取php變量的實現(xiàn)代碼
- PHP與javascript實現(xiàn)變量交互的示例代碼
- php和javascript之間變量的傳遞實現(xiàn)代碼
- 用php實現(xiàn)像JSP,ASP里Application那樣的全局變量
- php使用json_encode對變量json編碼
- php解析http獲取的json字符串變量總是空白null
- PHP變量賦值、代入給JavaScript中的變量
- PHP JSON格式數(shù)據(jù)交互實例代碼詳解
- 解析Extjs與php數(shù)據(jù)交互(增刪查改)
- PHP與JavaScript針對Cookie的讀寫、交互操作方法詳解
- php變量與JS變量實現(xiàn)不通過跳轉(zhuǎn)直接交互的方法
相關(guān)文章
PHP 關(guān)于訪問控制的和運算符優(yōu)先級介紹
這篇文章主要介紹了PHP中關(guān)于訪問控制的和運算符優(yōu)先級介紹,需要的朋友可以參考下2013-07-07
PHP開發(fā)工具ZendStudio下Xdebug工具使用說明詳解
我使用的是XAMPP的集成開發(fā)平臺環(huán)境。里面已經(jīng)預設(shè)了Xdebug的調(diào)試工具,只需要自己改下配置的就可以了2013-11-11
php實現(xiàn)parent調(diào)用父類的構(gòu)造方法與被覆寫的方法
這篇文章主要介紹了php實現(xiàn)parent調(diào)用父類的構(gòu)造方法與被覆寫的方法,在上一篇關(guān)于使用類繼承解決代碼重復問題的基礎(chǔ)上,進一步分析了parent調(diào)用父類的構(gòu)造方法與被覆寫的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-02-02
php學習之數(shù)據(jù)類型之間的轉(zhuǎn)換介紹
php學習之數(shù)據(jù)類型之間的轉(zhuǎn)換介紹,學習php的朋友可以參考下。2011-06-06

