主頁(yè) > 知識(shí)庫(kù) > PHP+AJAX 投票器功能

PHP+AJAX 投票器功能

熱門(mén)標(biāo)簽:福建高頻外呼防封系統(tǒng)哪家好 周口網(wǎng)絡(luò)回?fù)芡夂粝到y(tǒng) 400電話申請(qǐng)辦理 商丘外呼系統(tǒng)好處 隨州銷(xiāo)售電銷(xiāo)機(jī)器人公司 外呼系統(tǒng)人工客服 百度地圖標(biāo)注類(lèi)型是酒店 網(wǎng)絡(luò)電話400申請(qǐng) 全國(guó)各省地圖標(biāo)注點(diǎn)

終于到AJAX,翻譯過(guò)來(lái)就是”異步Javascript和XML”,他可以實(shí)現(xiàn)網(wǎng)頁(yè)內(nèi)容的部分加載,可提高用戶體驗(yàn)?,F(xiàn)在有很多網(wǎng)站都有用這技術(shù),反正你知道他能實(shí)現(xiàn)網(wǎng)頁(yè)的異步更新就差不多了。當(dāng)然下面的例子都相對(duì)簡(jiǎn)單,并沒(méi)有體現(xiàn)它這一特點(diǎn)~

投票器

新建文件【 AJAX投票.html】

html>
head>
  script type="text/javascript">
    // 這里是js代碼
    function getVote(int) {
      if (window.XMLHttpRequest) {
        // 創(chuàng)建 XMLHttpRequest 對(duì)象
        // IE7+, Firefox, Chrome, Opera, Safari 瀏覽器執(zhí)行的代碼
        xmlhttp = new XMLHttpRequest();
      } else {
        //IE6, IE5 瀏覽器執(zhí)行的代碼
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      // 監(jiān)聽(tīng)響應(yīng)
      xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState ==4  xmlhttp.status == 200) {
          // 找到 id 為 poll 的控件
          document.getElementById('poll').innerHTML = xmlhttp.responseText;
        }
      }
      // 向PHP腳本傳遞主要參數(shù)q
      xmlhttp.open("GET", "poll_vote.php?q=" + int, true);
      xmlhttp.send();
    }
  /script>
/head>
body>
  div id="poll">
    h3>你喜歡吃嗎?/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>

創(chuàng)建【poll_vote.php】腳本文件

?php 
  // 接收參數(shù)q
  $vote = htmlspecialchars($_REQUEST['q']);
  // 獲取文件中存儲(chǔ)的數(shù)據(jù)(這里需要在同一目錄下新建一個(gè)poll_result.txt文件)
  $filename = "poll_result.txt";
  $conn = file($filename);
  // 將數(shù)據(jù)分割到數(shù)組
  $array = explode("||", $conn[0]);
  $yes = $array[0];
  $no = $array[1];
  $count = $array[2];
  if ($vote == 0) {
    $yes += 1;
    $count += 1;
  }
  if ($vote == 1) {
    $no += 1;
    $count += 1;
  }
  // 將投票數(shù)據(jù)保存到文檔
  $insertvote = $yes . '||' . $no . '||' . $count;
  $fp = fopen($filename, "w");
  fputs($fp, $insertvote);
  fclose($fp);
 ?>
 h2>結(jié)果:/h2>
 table>
  tr>
    td>是:/td>
    td>
      span style="display: inline-block; background-color: green; width: ?php echo 100 * round($yes / ($yes + $no), 2);?>px; height: 20px;">/span>?php echo 100 * round($yes / ($yes + $no), 2); ?>%
    /td>
  /tr>
  tr>
    td>否:/td>
    td>
      span style="display: inline-block; background-color: red; width: ?php echo 100 * round($no / ($yes + $no), 2);?>px; height: 20px;">/span>?php echo 100 * round($no / ($yes + $no), 2); ?>%
    /td>
  /tr>
 /table>
 p>?php echo "參與人數(shù):" . $count; ?>/p>

新建一個(gè)空白的文檔 【poll_result.txt】

此時(shí)目錄:

|-AJAX投票.html
|-poll_vote.php
|-poll_result.txt

如果不同則需修改上面相應(yīng)的代碼

效果:


總結(jié)

以上所述是小編給大家介紹的PHP+AJAX 投票器功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

您可能感興趣的文章:
  • PHP+AJAX實(shí)現(xiàn)投票功能的方法

標(biāo)簽:十堰 定西 六安 樂(lè)山 迪慶 南寧 海南 佛山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP+AJAX 投票器功能》,本文關(guān)鍵詞  PHP+AJAX,投票,器,功能,PHP+AJAX,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PHP+AJAX 投票器功能》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于PHP+AJAX 投票器功能的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章