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的頁面寬度設為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的頁面寬度設為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)不通過跳轉直接交互的方法
相關文章
PHP 開發(fā)環(huán)境配置(Zend Server安裝)
運行安裝文件(ZendServer-CE-php-5.3.2-5.0.1-Windows_x86.exe)開始安裝,選項請參照我的選擇。2010-04-04php將print_r處理后的數(shù)據(jù)還原為原始數(shù)組的解決方法
下面小編就為大家?guī)硪黄猵hp中將print_r處理后的數(shù)據(jù)還原為原始數(shù)組的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考2016-11-11php+mysqli實現(xiàn)批量替換數(shù)據(jù)庫表前綴的方法
這篇文章主要介紹了php+mysqli實現(xiàn)批量替換數(shù)據(jù)庫表前綴的方法,涉及針對mysql數(shù)據(jù)庫的遍歷與表名修改等操作技巧,具有一定的參考借鑒價值,需要的朋友可以參考下2014-12-12Linux環(huán)境下php實現(xiàn)給網(wǎng)站截圖的方法
這篇文章主要介紹了Linux環(huán)境下php實現(xiàn)給網(wǎng)站截圖的方法,結合實例形式較為詳細的分析了Linux環(huán)境下php實現(xiàn)網(wǎng)站截圖的具體步驟與相關操作技巧,需要的朋友可以參考下2016-05-05