PHP Examples -AJAX Poll


AJAX Poll

In the example below, we will demonstrate a voting program through which voting results are displayed without the web page being refreshed.

Do you like PHP and AJAX?

Yes:
No:

Example Explained - HTML Page

When the user selects one of the options above, a function named "getVote()" is executed. This function is triggered by the "onclick" event.

poll.htmlThe file code is as follows:

<html>
<head>
<meta charset="utf-8">
<title>Example(example.com)</title>
<script>
function getVote(int) {
  if (window.XMLHttpRequest) {
    // IE7+, Firefox, Chrome, Opera, Safari 执行代码
    xmlhttp=new XMLHttpRequest();
  } else {
    // IE6, IE5 执行代码
    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>

The getVote() function performs the following steps:

  • Create an XMLHttpRequest object
  • Create a function that executes when the server response is ready
  • Send a request to the file on the server
  • Note the parameter (q) added to the end of the URL (containing the content of the dropdown list)

PHP File

The server page called via JavaScript above is a PHP file named "poll_vote.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>

When the selected value is sent from JavaScript to the PHP file, the following happens:

  1. Get the content of the "poll_result.txt" file
  2. Put the file content into variables, and add 1 to the selected variable
  3. Write the result to the "poll_result.txt" file
  4. Output the graphical voting result

Text File

The text file (poll_result.txt) stores the data from the voting program.

The data it stores is as follows:

3||4

The first number represents the number of "Yes" votes, and the second number represents the number of "No" votes.

Note:Remember to allow only your Web server to edit this text file.Do notgive access to anyone else, except the Web server (PHP).

Other Extensions