PHP開發(fā)基礎(chǔ)教學(xué)之AJAX投票
AJAX投票
在下面的實(shí)例中,我們將示範(fàn)一個(gè)投票程序,透過它,投票結(jié)果在網(wǎng)頁不進(jìn)行刷新的情況下被顯示。
?頁面顯示見右圖
當(dāng)使用者選擇上面的某個(gè)選項(xiàng)時(shí),會(huì)執(zhí)行名為 "getVote()" 的函數(shù)。此函數(shù)由 "onclick" 事件觸發(fā)。
本實(shí)例包含三部分
?HTML表單
?PHP檔案
#?TXT檔案
- #HTML檔:
<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)建在服務(wù)器響應(yīng)就緒時(shí)執(zhí)行的函數(shù) xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("poll").innerHTML=xmlhttp.responseText; } } //向服務(wù)器上的文件發(fā)送請求 xmlhttp.open("GET","2.php?vote="+int,true); xmlhttp.send(); } </script> </head> <body> <div id="poll"> <h3>你喜歡 PHP 和 AJAX 嗎?</h3> <!-- 用戶選擇一個(gè)選項(xiàng),觸發(fā)onclick事件,執(zhí)行g(shù)etVote()函數(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 頁麵包含一個(gè)簡單的HTML 表單,其中的<div> 元素帶有兩個(gè)單選按鈕。
- 表單這樣工作:
- 當(dāng)使用者選擇"yes" 或"no" 時(shí),會(huì)觸發(fā)一個(gè)事件
- 當(dāng)事件觸發(fā)時(shí),執(zhí)行g(shù)etVote() 函數(shù)
- 圍繞該表單的是名為"poll" 的<div>。當(dāng)資料從 getVote() 函數(shù)傳回時(shí),傳回的資料會(huì)取代該表單。
?建立XMLHttpRequest 物件
已建立在伺服器回應(yīng)就緒時(shí)執(zhí)行的函數(shù)
- ?向伺服器上的檔案傳送請求
- ?請注意新增至URL 末端的參數(shù)(q)(包含下拉清單的內(nèi)容)
- PHP檔案:
- 上面這段透過JavaScript 呼叫的伺服器頁面是名為"2.php" 的PHP 檔案:
<?php //過濾瀏覽器傳過來的數(shù)據(jù) $vote = htmlspecialchars($_REQUEST['vote']); // 獲取文件中存儲(chǔ)的數(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);//關(guān)閉打開文件 ?> <h2>結(jié)果:</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>當(dāng)所選的值從JavaScript 傳送到PHP 檔案時(shí),將會(huì)發(fā)生:
##取得" poll_result.txt" 文件的內(nèi)容
?把文件內(nèi)容放入變量,並向被選變數(shù)累加1?把結(jié)果寫入" poll_result.txt" 檔案?輸出圖形化的投票結(jié)果它類似這樣:?0||0
第一個(gè)數(shù)字表示 "Yes" 投票,第二個(gè)數(shù)字表示 "No" 投票。- 註解:記得只允許您的 web 伺服器來編輯該文字檔案。不要讓其他人獲得存取權(quán),除了 web 伺服器 (PHP)。
- ################學(xué)習(xí)心得############本範(fàn)例主要包含以下知識(shí)點(diǎn):############表單基礎(chǔ):單選按鈕############?onclick事件:在物件被點(diǎn)擊時(shí)發(fā)生###
?函數(shù)呼叫、函數(shù)傳值
AJAX XMLHttpRequest物件的建立、在伺服器回應(yīng)的時(shí)候執(zhí)行的函數(shù)、傳送請求到伺服器上的檔案:見1-5講學(xué)習(xí)心得
?HTML DOM getElementById()方法:傳回?fù)碛兄付↖D 的第一個(gè)物件的參考
# PHP對檔案的相關(guān)操作:
?file():把整個(gè)檔案讀到一個(gè)陣列中
?fopen():開啟檔案或者URL
fputs():寫入檔案
#?fclose():關(guān)閉一個(gè)開啟檔案
#?相關(guān)函數(shù):
htmlspecialchars():預(yù)先定義的字元轉(zhuǎn)換為HTML 實(shí)體
?explode():把字串打散為數(shù)組