Easy Tutorial
❮ Func String Print Func Filesystem Unlink ❯

PHP Example - AJAX Voting


AJAX Voting

In the following example, we will demonstrate a voting program through which voting results are displayed without refreshing the webpage.

Do you like PHP and AJAX?


Example Explanation - HTML Page

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

poll.html file code is as follows:

<html>
<head>
<meta charset="utf-8">
<title>tutorialpro.org(tutorialpro.org)</title>
<script>
function getVote(int) {
  if (window.XMLHttpRequest) {
    // Code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else {
    // Code for 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>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:


PHP File

The server page called by the JavaScript above is a PHP file named "poll_vote.php":

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

// Get data from the file
$filename = "poll_result.txt";
$content = file($filename);

// Split the data into an 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>Results:</h2>
<table>
  <tr>
  <td>Yes:</td>
  <td>
  &lt;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>
  &lt;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>
<?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 occurs:


Text File

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

It stores data 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 not allow others to gain access, except for the web server (PHP).

❮ Func String Print Func Filesystem Unlink ❯