PHP開發(fā)基礎教程之AJAX投票
AJAX投票
在下面的實例中,我們將演示一個投票程序,通過它,投票結果在網(wǎng)頁不進行刷新的情況下被顯示。
?頁面顯示見右圖
當用戶選擇上面的某個選項時,會執(zhí)行名為 "getVote()" 的函數(shù)。該函數(shù)由 "onclick" 事件觸發(fā)。
本實例包括三部分
?HTML表單
?PHP文件
?TXT文件
HTML文件:
源碼見1.php
<html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script> function getVote(int) { //創(chuàng)建 XMLHttpRequest 對象 if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari 執(zhí)行代碼 xmlhttp=new XMLHttpRequest(); } else { // IE6, IE5 執(zhí)行代碼 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } //創(chuàng)建在服務器響應就緒時執(zhí)行的函數(shù) xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("poll").innerHTML=xmlhttp.responseText; } } //向服務器上的文件發(fā)送請求 xmlhttp.open("GET","2.php?vote="+int,true); xmlhttp.send(); } </script> </head> <body> <div id="poll"> <h3>你喜歡 PHP 和 AJAX 嗎?</h3> <!-- 用戶選擇一個選項,觸發(fā)onclick事件,執(zhí)行getVote()函數(shù) --> <form> 是: <input type="radio" name="vote" value="0" onclick="getVote(this.value)"> <br>否: <input type="radio" name="vote" value="1" onclick="getVote(this.value)"> </form> </div> </body> </html>
上面的 HTML 頁面包含一個簡單的 HTML 表單,其中的 <div> 元素帶有兩個單選按鈕。
表單這樣工作:
當用戶選擇 "yes" 或 "no" 時,會觸發(fā)一個事件
當事件觸發(fā)時,執(zhí)行 getVote() 函數(shù)
圍繞該表單的是名為 "poll" 的 <div>。當數(shù)據(jù)從 getVote() 函數(shù)返回時,返回的數(shù)據(jù)會替代該表單。
getVote() 函數(shù)會執(zhí)行以下步驟:
?創(chuàng)建 XMLHttpRequest 對象
創(chuàng)建在服務器響應就緒時執(zhí)行的函數(shù)
?向服務器上的文件發(fā)送請求
?請注意添加到 URL 末端的參數(shù)(q)(包含下拉列表的內(nèi)容)
PHP文件:
上面這段通過 JavaScript 調(diào)用的服務器頁面是名為 "2.php" 的 PHP 文件:
<?php //過濾瀏覽器傳過來的數(shù)據(jù) $vote = htmlspecialchars($_REQUEST['vote']); // 獲取文件中存儲的數(shù)據(jù) $filename = "3.txt"; $content = file($filename); // 將數(shù)據(jù)分割到數(shù)組中 $array = explode("||", $content[0]); $yes = $array[0]; $no = $array[1]; if ($vote == 0) { $yes = $yes + 1; } if ($vote == 1) { $no = $no + 1; } // 插入投票數(shù)據(jù) $insertvote = $yes."||".$no; $fp = fopen($filename,"w");//寫入方式打開 fputs($fp,$insertvote);//將$insertvote寫入文件中 fclose($fp);//關閉打開文件 ?> <h2>結果:</h2> <table> <tr> <td>是:</td> <td> <span style="display: inline-block; background-color:green; width:<?php echo(100*round($yes/($no+$yes),2)); ?>px; height:20px;" ></span> <?php echo(100*round($yes/($no+$yes),2)); ?>% </td> </tr> <tr> <td>否:</td> <td> <span style="display: inline-block; background-color:red; width:<?php echo(100*round($no/($no+$yes),2)); ?>px; height:20px;"></span> <?php echo(100*round($no/($no+$yes),2)); ?>% </td> </tr> </table>
當所選的值從 JavaScript 發(fā)送到 PHP 文件時,將發(fā)生:
獲取 "poll_result.txt" 文件的內(nèi)容
?把文件內(nèi)容放入變量,并向被選變量累加 1
?把結果寫入 "poll_result.txt" 文件
?輸出圖形化的投票結果
TXT文件
文本文件 (3.txt) 中存儲來自投票程序的數(shù)據(jù)。
它類似這樣:
?0||0
第一個數(shù)字表示 "Yes" 投票,第二個數(shù)字表示 "No" 投票。
注釋:記得只允許您的 web 服務器來編輯該文本文件。不要讓其他人獲得訪問權,除了 web 服務器 (PHP)。
學習心得
本例子主要包括如下知識點:
表單基礎:單選按鈕
?onclick事件:在對象被點擊時發(fā)生
?函數(shù)調(diào)用、函數(shù)傳值
AJAX XMLHttpRequest對象的創(chuàng)建、在服務器響應的時候執(zhí)行的函數(shù)、向服務器上的文件發(fā)送請求:見1-5講學習心得
?HTML DOM getElementById()方法:返回對擁有指定 ID 的第一個對象的引用
?PHP對文件的相關操作:
?file():把整個文件讀入一個數(shù)組中
?fopen():打開文件或者 URL
fputs():寫入文件
?fclose():關閉一個打開文件
?相關函數(shù):
htmlspecialchars():預定義的字符轉換為 HTML 實體
?explode():把字符串打散為數(shù)組