How to create AJAX Poll in PHP?
Hi friends,
Extremely thanks for helping me last time. Now I am having one more problem. I am designing a web page in which I want to create a Poll by which the users can poll their opinion. But I don't have enough idea about the AJAX poll. So I need your help very immediately. Please tell me how to create AJAX Poll in PHP? It would be very great if you provide me some coding regarding the same.
Re: The getVote() Function in PHP?
The getVote() Function would be very useful for creating a poll using the PHP. The purpose of this function is to execute, when “yes” or “no” is selected in the HTML form. The following are some points related to the getVote() :
- It defines the url (filename) to send to the server.
- It also adds a random number to prevent the server from using a cached file.
- It adds a parameter (vote) to the url with the content of the input field.
- Opens the XMLHTTP object with the given url.
- Sends an HTTP request to the server.
Re: How to create AJAX Poll in PHP?
The HTML page contains a link to an external JavaScript, an HTML form, and a div element. The following example demonstrates the same :
HTML Code:
<html>
<head>
<script type="demo/javascript" src="poll.js"></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>
Re: JavaScript code for AJAX Poll in PHP?
The stateChanged() and GetXmlHttpObject functions are almost the same. The following is the JavaScript code stored in the file "poll.js" :
Code:
var xmlhttp;
function getVote(int)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="poll_vote.php";
url=url+"?vote="+int;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("poll").innerHTML=xmlhttp.responseText;
}
}
function GetXmlHttpObject()
{
var objXMLHttp=null;
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return objXMLHttp;
}
Re: How to create AJAX Poll in PHP?
The final step would be calling the server page by the JavaScript code is a simple PHP file called "poll_vote.php". The following code explains the same :
PHP Code:
<?php
$vote = $_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>Result:</h2>
<table>
<tr>
<td>Yes:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($yes/($no+$yes),2)); ?>'
height='20'>
<?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='20'>
<?php echo(100*round($no/($no+$yes),2)); ?>%
</td>
</tr>
</table>
Re: How to create AJAX Poll in PHP?
If I want to add one more option (here only yes or no given) to the above given code what are changes required to do.
Please help me to add more options to this code.
Thanks in Advance.
Re: How to create AJAX Poll in PHP?
These information were really nice, I really like, and also appreciate your work, awesome work, I hope that this will really help those who really wanted to create Ajax poll in PHP, I think that people will really like this information, and appreciate your work.
Re: How to create AJAX Poll in PHP?
You have given nice information. This code help me a lot.
I try this code it really nice and solve my problem.
I am sure many people will get help from this code.
Re: How to create AJAX Poll in PHP?
When a user choose an option above, a function called "getVote()" is executed. The function is triggered by the "onclick" event:
<html>
<head>
<script type="text/javascript">
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 so far?</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 does the following:
* Create an XMLHttpRequest object
* Create the function to be executed when the server response is ready
* Send the request off to a file on the server
* Notice that a parameter (vote) is added to the URL (with the value of the yes or no option)
The PHP File
<?php
$vote = $_REQUEST['vote'];
//get content of textfile
$filename = "poll_result.txt";
$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);
?>
<h2>Result:</h2>
<table>
<tr>
<td>Yes:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($yes/($no+$yes),2)); ?>'
height='20'>
<?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='20'>
<?php echo(100*round($no/($no+$yes),2)); ?>%
</td>
</tr>
</table>