php后臺(tái)程序與Javascript的兩種交互方式
更新時(shí)間:2009年10月25日 17:44:03 作者:
在網(wǎng)頁(yè)制作過(guò)程中怎樣在不刷新頁(yè)面的情況下使前臺(tái)頁(yè)面和后臺(tái)CGI頁(yè)面保持交互一直是個(gè)問(wèn)題。這里介紹兩個(gè)方法。
方法一:通過(guò)Cookie交互。
一共是三個(gè)文件,分別為:index.htm,action.php,main.htm
原理為前臺(tái)頁(yè)面main.htm和后臺(tái)action.php通過(guò)頁(yè)面框架 index.htm組織起來(lái),將action.php的頁(yè)面寬度設(shè)為0,這樣并不影響顯示。action.php將信息放入cookie中,main.htm通過(guò)讀取 cookie來(lái)實(shí)現(xiàn)交互。在main.htm中也可以通過(guò)重新讀取action.php 來(lái)實(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
<?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>
方法二:直接通過(guò)parent.*.*來(lái)實(shí)現(xiàn)交互。
一共是三個(gè)文件,分別為:index.htm,action.php,main.htm,其中index.htm和前面的一樣。
原理為通過(guò)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>
一共是三個(gè)文件,分別為:index.htm,action.php,main.htm
原理為前臺(tái)頁(yè)面main.htm和后臺(tái)action.php通過(guò)頁(yè)面框架 index.htm組織起來(lái),將action.php的頁(yè)面寬度設(shè)為0,這樣并不影響顯示。action.php將信息放入cookie中,main.htm通過(guò)讀取 cookie來(lái)實(shí)現(xiàn)交互。在main.htm中也可以通過(guò)重新讀取action.php 來(lái)實(shí)現(xiàn)控制后臺(tái)CGI程序。
index.htm
復(fù)制代碼 代碼如下:
<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
復(fù)制代碼 代碼如下:
<?php
srand((double)microtime()*1000000);
$result=rand(0,100);
setcookie("action",$result,time()+900,"/");
?>
main.htm
復(fù)制代碼 代碼如下:
<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>
方法二:直接通過(guò)parent.*.*來(lái)實(shí)現(xiàn)交互。
一共是三個(gè)文件,分別為:index.htm,action.php,main.htm,其中index.htm和前面的一樣。
原理為通過(guò)parent.rightFrame.test.current_cookie.value直接傳遞信息。
action.php
復(fù)制代碼 代碼如下:
<?
srand((double)microtime()*1000000);
$result=rand(0,100);
?>
<script language="javascript">
parent.rightFrame.test.current_cookie.value="<? echo $result?>";
</script>
main.htm
復(fù)制代碼 代碼如下:
<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>
您可能感興趣的文章:
- js獲取php變量的實(shí)現(xiàn)代碼
- PHP與javascript實(shí)現(xiàn)變量交互的示例代碼
- php和javascript之間變量的傳遞實(shí)現(xiàn)代碼
- 用php實(shí)現(xiàn)像JSP,ASP里Application那樣的全局變量
- php使用json_encode對(duì)變量json編碼
- php解析http獲取的json字符串變量總是空白null
- PHP變量賦值、代入給JavaScript中的變量
- PHP JSON格式數(shù)據(jù)交互實(shí)例代碼詳解
- 解析Extjs與php數(shù)據(jù)交互(增刪查改)
- PHP與JavaScript針對(duì)Cookie的讀寫(xiě)、交互操作方法詳解
- php變量與JS變量實(shí)現(xiàn)不通過(guò)跳轉(zhuǎn)直接交互的方法
相關(guān)文章
PHP 關(guān)于訪(fǎng)問(wèn)控制的和運(yùn)算符優(yōu)先級(jí)介紹
這篇文章主要介紹了PHP中關(guān)于訪(fǎng)問(wèn)控制的和運(yùn)算符優(yōu)先級(jí)介紹,需要的朋友可以參考下2013-07-07
PHP中Memcache操作類(lèi)及用法實(shí)例
這篇文章主要介紹了PHP中Memcache操作類(lèi)及用法,以實(shí)例形式詳細(xì)分析了Memcache類(lèi)連接數(shù)據(jù)庫(kù)及進(jìn)行緩存操作的具體用法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12
PHP開(kāi)發(fā)工具ZendStudio下Xdebug工具使用說(shuō)明詳解
我使用的是XAMPP的集成開(kāi)發(fā)平臺(tái)環(huán)境。里面已經(jīng)預(yù)設(shè)了Xdebug的調(diào)試工具,只需要自己改下配置的就可以了2013-11-11
php操作redis常見(jiàn)方法示例【key與value操作】
這篇文章主要介紹了php操作redis常見(jiàn)方法,結(jié)合實(shí)例形式分析了PHP針對(duì)Redis key與value的各種常見(jiàn)操作技巧,需要的朋友可以參考下2020-04-04
php實(shí)現(xiàn)parent調(diào)用父類(lèi)的構(gòu)造方法與被覆寫(xiě)的方法
這篇文章主要介紹了php實(shí)現(xiàn)parent調(diào)用父類(lèi)的構(gòu)造方法與被覆寫(xiě)的方法,在上一篇關(guān)于使用類(lèi)繼承解決代碼重復(fù)問(wèn)題的基礎(chǔ)上,進(jìn)一步分析了parent調(diào)用父類(lèi)的構(gòu)造方法與被覆寫(xiě)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02
php學(xué)習(xí)之?dāng)?shù)據(jù)類(lèi)型之間的轉(zhuǎn)換介紹
php學(xué)習(xí)之?dāng)?shù)據(jù)類(lèi)型之間的轉(zhuǎn)換介紹,學(xué)習(xí)php的朋友可以參考下。2011-06-06

