Step 1: Create a html page with your question
<div id="poll">
<h3>Do you like this post?</h3>
<form>
Yes: <input type="radio" name="vote" value="0" >
<br>No: <input type="radio" name="vote" value="1">
</form>
</div>
Step 2: Now, create a javascript function that will work onClick with following code
<script>
function pollVote(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); //your form submitted to poll_vote.php
xmlhttp.send();
}
</script>
Step 3: Update you HTML code as like below
<div id="poll">
<h3>Do you like this post?</h3>
<form>
Yes: <input type="radio" name="vote" value="0" onclick="pollVote(this.value)">
<br>No: <input type="radio" name="vote" value="1" onclick="pollVote(this.value)">
</form>
</div>
;
Step 4: Take a new php file in the name “poll_vote.php” that you already define into javascript function and write following code.
<?php
echo $vote = $_REQUEST['vote'];
?>
Step 5: Create a txt file where you will store your votes in the name “poll_result.txt”.
Step 6: Add you txt file name into PHP file and insert data into txt file using following code.
<?php
$vote = $_REQUEST['vote'];
$filename = "poll_result.txt"; //file name
//put content in array
$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;}
//insert votes to txt file
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>
Step 7: print your poll data
<h1>Result:</h1>
<table>
<tr>
<td>Yes:</td>
<td>
<img src="poll.gif" width='<?php echo(100*round($yes/($no+$yes),2)); ?>' height='10'> <?php echo(100*round($yes/($no+$yes),2)); ?>%
</td>
</tr>
<tr>
<td>No:</td>
<td>
<img src="poll.gif" width='<?php echo(100*round($no/($no+$yes),2)); ?>' height='10'> <?php echo(100*round($no/($no+$yes),2)); ?>%
</td>
</tr>
</table>
So, you final PHP code is-
<?php
$vote = $_REQUEST['vote'];
$filename = "poll_result.txt"; //file name
$content = file($filename);
//put content in array
$array = explode("||", $content[0]);
$yes = $array[0];
$no = $array[1];
if ($vote == 0){ $yes = $yes + 1;}
if ($vote == 1){ $no = $no + 1;}
//insert votes to txt file
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>
<h1>Result:</h1>
<table>
<tr>
<td>Yes:</td>
<td>
<img src="poll.gif" width='<?php echo(100*round($yes/($no+$yes),2)); ?>' height='10'> <?php echo(100*round($yes/($no+$yes),2)); ?>%
</td>
</tr>
<tr>
<td>No:</td>
<td>
<img src="poll.gif" width='<?php echo(100*round($no/($no+$yes),2)); ?>' height='10'> <?php echo(100*round($no/($no+$yes),2)); ?>%
</td>
</tr>
</table>
You can download full source code from here- Source code