WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP Example - AJAX Poll


In this part we learn AJAX Poll

AJAX Poll

In the following example, we will demonstrate a voting process, through which the voting results are displayed without refreshing the web page.

We can make PHP and AJAX so far?

Truly:
No:

Example explanation-HTML page

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

poll.html The file code is as follows:

<html>
<head>
<meta charset="utf-8">
<title>Novice Tutorial(runoob.com)</title>
<script>
function getVote(int) {
  if (window.XMLHttpRequest) {
    //IE7+, Firefox, Chrome, Opera, Safari Execute code
    xmlhttp=new XMLHttpRequest();
  } else {
    //IE6, IE5 Execute code
    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>Do you like PHP and AJAX?</h3>
<form>
Yes:
<input type="radio" name="vote" value="0" onclick="getVote(this.value)">
<br>no:
<input type="radio" name="vote" value="1" onclick="getVote(this.value)">
</form>
</div>

</body>
</html>

The getVote () function performs the following steps:

  • Creating an XMLHttpRequest object
  • Create a function to be executed when the server response is ready
  • Send a request to a file on the server
  • Please note the parameter (q) added to the end of the URL (including the content of the drop-down list)

PHP file

The above server page called via JavaScript is named "poll_vote.php" 的 PHP 文件:

<?php
$vote = htmlspecialchars($_REQUEST['vote']);

//Get data stored in a file
$filename = "poll_result.txt";
$content = file($filename);

//Split data into array
$array = explode("||", $content[0]);
$yes = $array[0];
$no = $array[1];

if ($vote == 0)
{
  $yes = $yes + 1;
}

if ($vote == 1)
{
  $no = $no + 1;
}

//Insert voting data
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>

<h2>result:</h2>
<table>
  <tr>
  <td>Yes:</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>no:</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 a PHP file, it will happen:

  1. Get the contents of the "poll_result.txt" file
  2. Put the contents of the file into a variable and accumulate 1 to the selected variable
  3. Write results to "poll_result.txt" file
  4. Output graphical voting results

Text file

Text file(poll_result.txt)Stores data from voting procedures.

The data it stores looks like this:

3||4

The first number indicates the number of votes for "Yes" and the second number indicates the number of votes for "No".

Note: Remember to allow only your web server to edit the text file. Don't give others access, except the web server (PHP).