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

  • <li id="8bibw"></li>
    <rt id="8bibw"><tr id="8bibw"></tr></rt>
    <i id="8bibw"></i>
    1. <i id="8bibw"></i>

      Ajax voting by pHP novice entry

      AJAX Voting

      #In the following example, we will demonstrate a voting program through which the voting results are displayed on the web page without refreshing The situation is displayed

      First we write a php file, the code is as follows:

      <!DOCTYPE html>
      <html>
      <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","demo.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>

      Code explanation:

      getVote() function will perform the following steps:

      Create an XMLHttpRequest object

      Create a function that is executed when the server response is ready

      Send a request to a file on the server

      Please note the parameters added to the end of the URL (q) (Contains the contents of the drop-down list)

      Then create a demo.php file. The code is as follows:

      <?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>

      Code explanation:

      When the selected value is retrieved from JavaScript When sending to a PHP file, what will happen:

      1. Get the contents of the "poll_result.txt" file

      2. Put the file contents into a variable and add 1## to the selected variable

      #3. Write the results to the "poll_result.txt" file

      4. Output the graphical voting results


      Continuing Learning
      ||
      <!DOCTYPE html> <html> <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","7_2.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>
      submitReset Code