国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

PHP - AJAX 投票

AJAX 投票

在下面的實例中,我們將示範一個投票程序,透過它,投票結果在網頁不進行刷新的情況下被顯示。

<!DOCTYPE html>
 <html lang="en">
 <head>
     <meta charset="UTF-8">
     <title>PHP中文網(php.cn)</title>
     <script>
         function getVote(int) {
             if (window.XMLHttpRequest) {
                 // IE7+, Firefox, Chrome, Opera, Safari 執(zhí)行代碼
                 xmlhttp=new XMLHttpRequest();
             } else {
                 // IE6, IE5 執(zhí)行代碼
                 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
             }
             xmlhttp.onreadystatechange=function() {
                 if (xmlhttp.readyState==4 && xmlhttp.status==200)
                 {
                     document.getElementById("poll").innerHTML=xmlhttp.responseText;
                 }
             }
             xmlhttp.open("GET","poll_vote.php?vote="+int,true);
             xmlhttp.send();
         }
     </script>
 </head>
 <body>
 <div id="poll">
     <h3>你喜歡 PHP 和 AJAX 嗎?</h3>
     <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>

0.png

當使用者選擇上面的某個選項時,會執(zhí)行名為 "getVote()" 的函數。此函數由 "onclick" 事件觸發(fā)。

getVote() 函數會執(zhí)行下列步驟:

· ? 建立XMLHttpRequest 物件

· ? 建立在伺服器回應就緒時執(zhí)行的函數

· ? 向伺服器上的檔案傳送請求

· ? 請注意新增至URL 末端的參數(q)(包含下拉清單的內容)


PHP 檔案? ? ?

上面這段透過JavaScript 呼叫的伺服器頁面是名為"poll_vote.php" 的PHP 檔案:

<?php
 $vote = htmlspecialchars($_REQUEST['vote']);
 
 // 獲取文件中存儲的數據
 $filename = "poll_result.txt";
 $content = file($filename);
 
 // 將數據分割到數組中
 $array = explode("||", $content[0]);
 $yes = $array[0];
 $no = $array[1];
 
 if ($vote == 0)
 {
     $yes = $yes + 1;
 }
 
 if ($vote == 1)
 {
     $no = $no + 1;
 }
 
 // 插入投票數據
 $insertvote = $yes."||".$no;
 $fp = fopen($filename,"w");
 fputs($fp,$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 傳送到PHP 檔案時,將發(fā)生:

1.??? 取得"poll_result.txt" 檔案的內容

2.??? 將檔案內容放入變量,並累積已被選取變數為累加1

#3.把結果寫入"poll_result.txt" 檔案

4.??? 輸出圖形化的投票結果


文字檔



### ###文字檔案(poll_result.txt)中儲存來自投票程式的資料。 ######它儲存的資料如下所示:############5||3###############第一個數字表示"Yes" 的投票數,第二個數字表示"No" 的投票數。 #########註解###:請記得只允許您的 Web 伺服器來編輯該文字檔案。不要讓其他人獲得存取權,除了 Web 伺服器 (PHP)。 ############
繼續(xù)學習
||
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>PHP中文網(php.cn)</title> <script> function getVote(int) { if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari 執(zhí)行代碼 xmlhttp=new XMLHttpRequest(); } else { // IE6, IE5 執(zhí)行代碼 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("poll").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","poll_vote.php?vote="+int,true); xmlhttp.send(); } </script> </head> <body> <div id="poll"> <h3>你喜歡 PHP 和 AJAX 嗎?</h3> <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>