Creating a Dynamic Poll with jQuery and PHP
In Javascript / AJAX, PHP by Jonathan RudenbergWhen you combine some neat functionality courtesy of PHP with the cleverness of jQuery you can produce some pretty cool results. In this tutorial we'll create a poll using PHP and XHTML, then make use of some jQuery Ajax effects to eliminate the need for a page refresh, and to give it a nice little bit of animation.
HTML
Let’s get our <head> set up:
<link href="style.css" rel="stylesheet" type="text/css" /> <script src="jquery.js" type="text/javascript" charset="utf-8"></script> <script src="jquery.cookie.js" type="text/javascript" charset="utf-8"></script> <script src="poll.js" type="text/javascript" charset="utf-8"></script>
- style.css will hold the CSS markup.
- jquery.js is the base jQuery library.
- jquery.cookie.js is a plugin by Klaus Hartl to add cookie manipulation to jQuery.
- poll.js will have the Javascript that makes the poll dynamic.
Next, we’ll create a simple poll form:

<div id="poll-container">
<h3>Poll</h3>
<form id='poll' action="poll.php" method="post" accept-charset="utf-8">
<p>Pick your favorite Javascript framework:</p>
<p><input type="radio" name="poll" value="opt1" id="opt1" /><label for='opt1'> jQuery</label><br />
<input type="radio" name="poll" value="opt2" id="opt2" /><label for='opt2'> Ext JS</label><br />
<input type="radio" name="poll" value="opt3" id="opt3" /><label for='opt3'> Dojo</label><br />
<input type="radio" name="poll" value="opt4" id="opt4" /><label for='opt4'> Prototype</label><br />
<input type="radio" name="poll" value="opt5" id="opt5" /><label for='opt5'> YUI</label><br />
<input type="radio" name="poll" value="opt6" id="opt6" /><label for='opt6'> mootools</label><br /><br />
<input type="submit" value="Vote →" /></p>
</form>
</div>
This form will be processed by the PHP for now, and when we get the Javascript running, by jQuery. The PHP and Javascript are designed to pull the option ID from the value tag. is just a HTML entity encoded space, and → is an arrow: →.
PHP
Introduction
If Javascript is disabled, the PHP will:
- Take GET/POST requests from the form
- Set/check a cookie
- Make sure the request is from a unique IP
- Store the vote in a flat file DB
- Return the results included with a HTML file
If Javascript is enabled, the PHP will:
- Take GET/POST requests from the Javascript
- Make sure the request is from a unique IP
- Store the vote in a flat file DB
- Return the results as JSON
For the flat file DB we will be using a package written by Luke Plant.
First, we need an array with the names and IDs of the poll options:
<?php $options[1] = 'jQuery'; $options[2] = 'Ext JS'; $options[3] = 'Dojo'; $options[4] = 'Prototype'; $options[5] = 'YUI'; $options[6] = 'mootools';
The flatfile package uses numbers for the column identifiers, so lets set some constants to convert those to names:
define('OPT_ID', 0);
define('OPT_TITLE', 1);
define('OPT_VOTES', 2);
When the form is submitted, PHP needs to know what file to insert the results into and return, so we set another constant:
define('HTML_FILE', 'index.html');
We need to include flatfile.php and initialize a database object:
require_once('flatfile.php');
$db = new Flatfile();
The flat files are just text files stored in the data directory:
$db->datadir = 'data/';
define('VOTE_DB', 'votes.txt');
define('IP_DB', 'ips.txt');
If we get a request with the poll parameter, it’s the static form, so we process it. If the request has a vote parameter in it, it’s a Ajax request. Otherwise, we just return the HTML_FILE.
if ($_GET['poll'] || $_POST['poll']) {
poll_submit();
}
else if ($_GET['vote'] || $_POST['vote']) {
poll_ajax();
}
else {
poll_default();
}
poll_default()
function poll_default() {
global $db;
$ip_result = $db->selectUnique(IP_DB, 0, $_SERVER['REMOTE_ADDR']);
if (!isset($_COOKIE['vote_id']) && empty($ip_result)) {
print file_get_contents(HTML_FILE);
}
else {
poll_return_results($_COOKIE['vote_id']);
}
}
poll_default() processes requests directly to the script with no valid GET/POST requests.
The global line makes the $db object available in the function’s scope.
The script tracks unique IPs to make sure you can only vote once, so we do a query to check whether it is in the DB:
$ip_result = $db->selectUnique(IP_DB, 0, $_SERVER['REMOTE_ADDR']);
If we don’t have a cookie and the IP query comes up empty, the client hasn’t voted yet, so we can just send the HTML file which contains the form. Otherwise, we just send the results:
if (!isset($_COOKIE['vote_id']) && empty($ip_result)) {
print file_get_contents(HTML_FILE);
}
else {
poll_return_results($_COOKIE['vote_id']);
}
poll_submit()
function poll_submit() {
global $db;
global $options;
$id = $_GET['poll'] || $_POST['poll'];
$id = str_replace("opt", '', $id);
$ip_result = $db->selectUnique(IP_DB, 0, $_SERVER['REMOTE_ADDR']);
if (!isset($_COOKIE['vote_id']) && empty($ip_result)) {
$row = $db->selectUnique(VOTE_DB, OPT_ID, $id);
if (!empty($row)) {
$ip[0] = $_SERVER['REMOTE_ADDR'];
$db->insert(IP_DB, $ip);
setcookie("vote_id", $id, time()+31556926);
$new_votes = $row[OPT_VOTES]+1;
$db->updateSetWhere(VOTE_DB, array(OPT_VOTES => $new_votes), new SimpleWhereClause(OPT_ID, '=', $id));
poll_return_results($id);
}
else if ($options[$id]) {
$ip[0] = $_SERVER['REMOTE_ADDR'];
$db->insert(IP_DB, $ip);
setcookie("vote_id", $id, time()+31556926);
$new_row[OPT_ID] = $id;
$new_row[OPT_TITLE] = $options[$id];
$new_row[OPT_VOTES] = 1;
$db->insert(VOTE_DB, $new_row);
poll_return_results($id);
}
}
else {
poll_return_results($id);
}
}
poll_submit() takes the form submission, checks if the client has already voted, and then updates the DB with the vote.
These lines get the selected option’s ID, and set $id to it:
$id = $_GET['poll'] || $_POST['poll'];
$id = str_replace("opt", '', $id);
We need to check whether the option is in the DB yet:
$row = $db->selectUnique(VOTE_DB, OPT_ID, $id);
If it is in the DB (result not empty), we need to run an updateSetWhere(). If it isn’t we need to do an insert():
if (!empty($row)) {
$new_votes = $row[OPT_VOTES]+1;
$db->updateSetWhere(VOTE_DB, array(OPT_VOTES => $new_votes), new SimpleWhereClause(OPT_ID, '=', $id));
poll_return_results($id);
}
else if ($options[$id]) {
$new_row[OPT_ID] = $id;
$new_row[OPT_TITLE] = $options[$id];
$new_row[OPT_VOTES] = 1;
$db->insert(VOTE_DB, $new_row);
poll_return_results($id);
}
Either way, we need to insert the IP into the DB, and set a cookie (expires in one year):
$ip[0] = $_SERVER['REMOTE_ADDR'];
$db->insert(IP_DB, $ip);
setcookie("vote_id", $id, time()+31556926);
poll_return_results()
function poll_return_results($id = NULL) {
global $db;
$html = file_get_contents(HTML_FILE);
$results_html = "<div id='poll-container'><div id='poll-results'><h3>Poll Results</h3>\n<dl class='graph'>\n";
$rows = $db->selectWhere(VOTE_DB,
new SimpleWhereClause(OPT_ID, "!=", 0), -1,
new OrderBy(OPT_VOTES, DESCENDING, INTEGER_COMPARISON));
foreach ($rows as $row) {
$total_votes = $row[OPT_VOTES]+$total_votes;
}
foreach ($rows as $row) {
$percent = round(($row[OPT_VOTES]/$total_votes)*100);
if (!$row[OPT_ID] == $id) {
$results_html .= "<dt class='bar-title'>". $row[OPT_TITLE] ."</dt><dd class='bar-container'><div id='bar". $row[OPT_ID] ."'style='width:$percent%;'> </div><strong>$percent%</strong></dd>\n";
}
else {
$results_html .= "<dt class='bar-title'>". $row[OPT_TITLE] ."</dt><dd class='bar-container'><div id='bar". $row[OPT_ID] ."' style='width:$percent%;background-color:#0066cc;'> </div><strong>$percent%</strong></dd>\n";
}
}
$results_html .= "</dl><p>Total Votes: ". $total_votes ."</p></div></div>\n";
$results_regex = '/<div id="poll-container">(.*?)<\/div>/s';
$return_html = preg_replace($results_regex, $results_html, $html);
print $return_html;
}
poll_return_results() generates the poll results, takes the HTML file, replaces the form with the results, and returns the file to the client.
First, lets grab the HTML file and set $html to it:
$html = file_get_contents(HTML_FILE);
Next, we start the results HTML structure:
$results_html = "<div id='poll-container'><div id='poll-results'><h3>Poll Results</h3>\n<dl class='graph'>\n";
To create the results HTML we need to get all the rows (options) from the DB sorted by number of votes:
$rows = $db->selectWhere(VOTE_DB, new SimpleWhereClause(OPT_ID, "!=", 0), -1, new OrderBy(OPT_VOTES, DESCENDING, INTEGER_COMPARISON));
We also need the total votes to calculate percentages:
foreach ($rows as $row) {
$total_votes = $row[OPT_VOTES]+$total_votes;
}
Next, we calculate the percentage of votes the current option has:
foreach ($rows as $row) {
$percent = round(($row[OPT_VOTES]/$total_votes)*100);
The HTML for the results will be a definition list (<dl>) styled with CSS to create bar graphs:
$results_html .= "<dt class='bar-title'>". $row[OPT_TITLE] ."</dt><dd class='bar-container'><div id='bar". $row[OPT_ID] ."'style='width:$percent%;'> </div><strong>$percent%</strong></dd>\n";
Also, we should check if the current option is the one the client voted for, and change the color:
if (!$row[OPT_ID] == $id) {
}
else {
$results_html .= "<dt class='bar-title'>". $row[OPT_TITLE] ."</dt><dd class='bar-container'><div id='bar". $row[OPT_ID] ."' style='width:$percent%;background-color:#0066cc;'> </div><strong>$percent%</strong></dd>\n";
}
Here, we add a total vote count and close the html tags:
$results_html .= "</dl><p>Total Votes: ". $total_votes ."</p></div></div>\n";
This is a regex that finds the poll-container <div>:
$results_regex = '/<div id="poll-container">(.*?)<\/div>/s';
The last step in this function is to replace the poll form with the results using the regex, and return the result:
$return_html = preg_replace($results_regex, $results_html, $html); print $return_html;
poll_ajax()
function poll_ajax() {
global $db;
global $options;
$id = $_GET['vote'] || $_POST['vote'];
$ip_result = $db->selectUnique(IP_DB, 0, $_SERVER['REMOTE_ADDR']);
if (empty($ip_result)) {
$ip[0] = $_SERVER['REMOTE_ADDR'];
$db->insert(IP_DB, $ip);
if ($id != 'none') {
$row = $db->selectUnique(VOTE_DB, OPT_ID, $id);
if (!empty($row)) {
$new_votes = $row[OPT_VOTES]+1;
$db->updateSetWhere(VOTE_DB, array(OPT_VOTES => $new_votes), new SimpleWhereClause(OPT_ID, '=', $id));
}
else if ($options[$id]) {
$new_row[OPT_ID] = $id;
$new_row[OPT_TITLE] = $options[$id];
$new_row[OPT_VOTES] = 1;
$db->insert(VOTE_DB, $new_row);
}
}
}
$rows = $db->selectWhere(VOTE_DB, new SimpleWhereClause(OPT_ID, "!=", 0), -1, new OrderBy(OPT_VOTES, DESCENDING, INTEGER_COMPARISON));
print json_encode($rows);
}
poll_ajax() takes a request from the Javascript, adds the vote to the DB, and returns the results as JSON.
There are a few lines of code that are different from poll_submit(). The first checks if the Javascript just wants the results, and no vote should be counted:
if ($id != 'none')
The other two lines select the whole DB and return it as JSON:
$rows = $db->selectWhere(VOTE_DB, new SimpleWhereClause(OPT_ID, "!=", 0), -1, new OrderBy(OPT_VOTES, DESCENDING, INTEGER_COMPARISON)); print json_encode($rows);
CSS
.graph {
width: 250px;
position: relative;
right: 30px;
}
.bar-title {
position: relative;
float: left;
width: 104px;
line-height: 20px;
margin-right: 17px;
font-weight: bold;
text-align: right;
}
.bar-container {
position: relative;
float: left;
width: 110px;
height: 10px;
margin: 0px 0px 15px;
}
.bar-container div {
background-color:#cc4400;
height: 20px;
}
.bar-container strong {
position: absolute;
right: -32px;
top: 0px;
overflow: hidden;
}
#poll-results p {
text-align: center;
}

This CSS styles the results returned by the PHP or Javascript.
- .graph styles the container for the bars, titles and percentages. The
widthwill be different for each site. - .bar-title styles the titles for the bar graphs.
- .bar-container styles the individual bar and percentage containers
- .bar-container div styles the div that the bar is applied to. To create the bars, a percentage
widthis set with PHP or Javascript. - .bar-container strong styles the percentage.
- #poll-results p styles the total votes.
Javascript
Introduction
The Javascript will intercept the submit button, send the vote with Ajax, and animate the results.
First, some global variables. You should recognize the first three from the PHP. votedID stores the ID of the option the client voted for.
var OPT_ID = 0; var OPT_TITLE = 1; var OPT_VOTES = 2; var votedID;
Now we need a jQuery ready function which runs when the page loads:
$(document).ready(function(){
Inside that function we register the handler for the vote button which will run formProcess when it is triggered:
$("#poll").submit(formProcess);
We also need to check if the results <div> exists, and animate the results if it does:
if ($("#poll-results").length > 0 ) {
animateResults();
}
If we have a cookie we should jump straight to generating the results because the user has already voted. To do that we need to get rid of the poll form, get the id from the cookie, grab the results from the PHP and pass them to loadResults().
if ($.cookie('vote_id'))
$("#poll-container").empty();
votedID = $.cookie('vote_id');
$.getJSON("poll.php?vote=none",loadResults);
}
formProcess()
function formProcess(event){
event.preventDefault();
var id = $("input[@name='poll']:checked").attr("value");
id = id.replace("opt",'');
$("#poll-container").fadeOut("slow",function(){
$(this).empty();
votedID = id;
$.getJSON("poll.php?vote="+id,loadResults);
$.cookie('vote_id', id, {expires: 365});
});
}
formProcess() is called by the submit event which passes it an event object. It prevents the form from doing a normal submit, checks/sets the cookies, runs an Ajax submit instead, then calls loadResults() to convert the results to HTML.
First, we need to prevent the default action (submitting the form):
event.preventDefault();
Next, we get the ID from the currently selected option:
var id = $("input[@name='poll']:checked").attr("value");
id = id.replace("opt",'');
input[@name='poll']:checked is a jQuery selector that selects a <input> with an attribute of name='poll' that is checked. attr("value") gets the value of the object which in our case is optn where n is the ID of the option.
Now that we have the ID, we can process it. To start, we fade out the poll form, and setup an anonymous function as a callback that is run when the fade is complete. Animations don’t pause the script, so weird things happen if you don’t do it this way.
$("#poll-container").fadeOut("slow",function(){
After it has faded out we can delete the form from the DOM using empty():
$(this).empty();
In this case, $(this) is jQuery shorthand for the DOM element that the fade was applied to.
jQuery has some other shortcut functions, including $.getJSON() which does GET request for a JSON object. When we have the object, we call loadResults() with it:
$.getJSON("poll.php?vote="+id,loadResults);
The last thing to do is set the cookie:
$.cookie('vote_id', id, {expires: 365});
loadResults()
function loadResults(data) {
var total_votes = 0;
var percent;
for (id in data) {
total_votes = total_votes+parseInt(data[id][OPT_VOTES]);
}
var results_html = "<div id='poll-results'><h3>Poll Results</h3>\n<dl class='graph'>\n";
for (id in data) {
percent = Math.round((parseInt(data[id][OPT_VOTES])/parseInt(total_votes))*100);
if (data[id][OPT_ID] !== votedID) {
results_html = results_html+"<dt class='bar-title'>"+data[id][OPT_TITLE]+"</dt><dd class='bar-container'><div id='bar"+data[id][OPT_ID]+"'style='width:0%;'> </div><strong>"+percent+"%</strong></dd>\n";
} else {
results_html = results_html+"<dt class='bar-title'>"+data[id][OPT_TITLE]+"</dt><dd class='bar-container'><div id='bar"+data[id][OPT_ID]+"'style='width:0%;background-color:#0066cc;'> </div><strong>"+percent+"%</strong></dd>\n";
}
}
results_html = results_html+"</dl><p>Total Votes: "+total_votes+"</p></div>\n";
$("#poll-container").append(results_html).fadeIn("slow",function(){
animateResults();});
}
loadResults() is called by $.getJSON() and is passed a JSON object containing the results DB. It is pretty much the same as it’s PHP counterpart poll_return_results() with a few exceptions. The first difference is that we set the width on all the bars to 0% because we will be animating them. The other difference is that we are using a jQuery append() instead of regex to show the results. After the results fade in, the function calls animateResults().
animateResults()
function animateResults(){
$("#poll-results div").each(function(){
var percentage = $(this).next().text();
$(this).css({width: "0%"}).animate({
width: percentage}, 'slow');
});
}
animateResults() iterates through each of the bars and animates the width property based on the percentage.
each() is a jQuery function that iterates through each element that is selected:
$("#poll-results div").each(function(){
First, we set the percentage to the text of the element next to the bar which is the <strong> containing the percentage.
var percentage = $(this).next().text();
Then we make sure the width is set to 0%, and animate it:
$(this).css({width: "0%"}).animate({
width: percentage}, 'slow');
Comments
Leave a CommentAdd a Comment














Hairudy
July 23rd, 2008
Wow…great bro…
thanks…
Bhaarat
July 23rd, 2008
I am first and this article just kicked major ass. I am not a fan of php so I am going to do this via struts2 and jsp. but the concept was awesome.
I look forward to every article in NETUTS!!
pavs
July 23rd, 2008
The end result looks really neat.
Even though I understand the main goal of this tuts is to make us understand, how to do it and a look at under the hood; but it seems a little bit too much work a poll.
Anyone knows and good wordpress poll plugin out there?
I have a suggestion for a future tut, using jquery plugin jCarousel, to make sliders. Very interesting stuff.
Thanks
Bruce Alrighty
July 23rd, 2008
Very Nice!
Nate
July 23rd, 2008
Sweetness…
Mad-Hatter
July 23rd, 2008
brilliant article mate, nice use of OOP in the PHP script excellent job! I hope to see more Jquery integration tutorials with PHP in the future!
Braden Keith
July 23rd, 2008
Nice effect
Jbcarey
July 24th, 2008
Okay, I like the result, but I think a little more explanation in between the step’s is neccesary…
Alex Coleman
July 24th, 2008
Awesome…will definitely come in handy. A bit difficult to grasp every detail, but that’s just the nature of the beast.
Shane
July 24th, 2008
nice transitions on the demo - thanks for posting. I’m typically a C# .NET developer (but I use a lot of jQuery).
Always wanting to learn more PHP - so thanks for this.
Lamin Barrow
July 24th, 2008
Great article.. what i love about it is that it is not based on some wordpress plugin or something. Thanks.
Ben Griffiths
July 24th, 2008
This is really good, and doesn’t need a massive amount of code either - thanks
Another Blog
July 24th, 2008
Really nice, we need to see more of these tutorials at net tuts! We are unternurished unlike those over at PSDTUTS and VECTORTUTS!
Andrew
Mark Abucayon
July 24th, 2008
wow awesome, exactly I am searching example poll now I think I will used this one later. Really nice I like it very much. Thank you for sharing this one
Daniel
July 24th, 2008
very useful tutorial ! thanks so much! keep up the good work.
Sebastian
July 24th, 2008
awesome work, but whatever i do it still shows me the SAME results! =(
jeeremie
July 24th, 2008
Wow! I love Nettuts. It is so easy to go through your tutorials here. And besides, all the scripts you create are so useful. I wish I had read this article yesterday as I was looking for inserting a poll on my blog. Too late, that’s done. But thanks anyway.
BeyondRandom
July 24th, 2008
you cant see me…but im bowing down right lol…as usual, thanks alot!
Javier Tapia
July 24th, 2008
¡¡awesome tutorial!! thanks
Taylor Satula
July 24th, 2008
The submit button and radio buttons should be styled.
But besides that very nice
Michael Thompson
July 24th, 2008
@Lamin Barrow: Yes, thank god. Wordpress is far from the only web technology worth writing tutorials about.
Ty
July 24th, 2008
I downloaded the Source file and placed it in my Aptana Studio setup.
It’s not returning the results, in my local IDE setup then, wondering what may need tweaked so the results will return?
Maybe placed on an actual web server it would of worked no fuss, I’m kind of new with Aptana.
Thanks,
Ty
Ty
July 24th, 2008
Nevermind, sorry for the dumb question, I haven’t fully setup my Aptana Studio setup by adding a Xampp server:
Introduction
Currently, to preview PHP pages and have them rendered appropriately, you will need to set up an external web server, such as XAMPP, and use that web server to render your PHP.
You can download XAMPP from the following web site:
http://www.apachefriends.org/en/xampp.html
Duh… OK Xampp is the way to go, I already know that.
Ad Manager
July 24th, 2008
Wow, I tried the demo and it’s indeed elegant and simple.
calvin
July 24th, 2008
jquery has dominated nettuts over the last couple weeks, and i couldn’t be happier. it’s such a great tool for designers, thanks so much guys!
Andrew
July 24th, 2008
This is the second time this week nettuts wrote an article about a feature i was trying to add. Awesome stuff!
Jonathan
July 24th, 2008
This is great. Didn’t grasp every detail on my first read through, so will have to come back for a more in depth look - but, it looks solid and will come in handy as I hate plugins.
Eric
July 24th, 2008
Really nice job, Jonathan thanks alot
The Frosty
July 24th, 2008
Woow.. JQuery pros? whats up?
I might have to start stopping by this site more often!
Faraz
July 25th, 2008
¡awesome tutorial!! thanks
pavs
July 26th, 2008
As much as I hate to say this, converting this to wordpress plugin would have been so sweet.
Accelebrate
July 29th, 2008
I’m having trouble installing this on my own server, I’ve copied all the files over and changed permission where I deemed necessary. The poll with radio buttons appears, I click on vote and it fades out nicely only to be replaced by nothing rather than the poll results. Stays that way until I delete the cookie and then the radio buttons just show again.
Have I missed a setting or something?
Accelebrate
July 29th, 2008
Closer inspection reveals an error…
Fatal error: Call to undefined function: json_encode() in poll.php on line 64
Nik
July 29th, 2008
Just found it. This is exactly what I needed for a project of mine. Haven’t try it, but look neat and beautiful.
Thanks indeed!
andol
July 30th, 2008
oh, awesome work~
packing all the js,css,html and php code into a baggage example would be better.
nicken
July 30th, 2008
Exactly what I am looking for.
However: I was having similar issues to Accelebrate, until I played around with permissions.
Now I am getting the same problem as Sebastian… the results aren’t changing.
Ryan
July 31st, 2008
I really like how the demo works.
I downloaded the code and tried to get it to work on my web server, and I seem to have the same problems as the people above. I changed the write permissions on votes.txt and ips.txt, but I still cannot get the votes to register.
I haven’t had the time to go through and study the code yet. Some instructions for installation would be helpful. Has anybody been successful getting this to work?
Ryan
July 31st, 2008
Actually, now that I look at the demo…. it’s not working properly either. It’s not registering any votes. Can anyone troubleshoot this?
Jonathan
August 2nd, 2008
I am having the same problem on my setup. I get the poll, select a selector and then fades to a blank screen. I don’t program (a designer only) but I chmod all files to 755 and even took it straight out of the zip, same results, I get a blank screen instead of the polling data!
I think the script is awesome (as demoed) and really would like to add it to my design arsenal.
Thanks in advance for any help you can give!
Jonathan
P. S. The script is located at http://www.adeeperfaith.com/inc/poll
Jonathan
August 2nd, 2008
I should have said “the same problem as accelabrate” (smile)
Connor
August 3rd, 2008
That’s a nice effect
STK
August 4th, 2008
i have to say that i have the same problem as mentioned above … what permissions should i set to store the poll data in the txt file? awesome script anyway …
would implement it in my new project so please help
Jonathan Rudenberg
August 4th, 2008
If you are having issues with the script please make sure you are running PHP 5.2.x, as it is not tested with PHP 4.
STK
August 5th, 2008
thank you for the hint. the version of my php ist 5.2.6 so it should work, but it don’t save the the data in the txt file - any other idea? … am i the only one where the script don’t run?
MyTivu - Free Video Site
August 6th, 2008
Great script. Will find a great use for it.
Taylor Satula
August 6th, 2008
I dont do many polls but still this is helpful
Ted
August 7th, 2008
First, great tutorial, best that ive seen by far.
Second
Im in the same situation as STK. I have php 5.2.6 on he server. And when one votes, the data stays the same, when i went to the txt file to clear the votes, and then votes again, it did not records my vote.
any help, or pointing me to the right direction,
greatly appreciated!
Nathan
August 11th, 2008
For people having issues. I tried the demo on a server with php version 5.2.5 and another running 5.2.6. Runs great on the 5.2.5 server, but 5.2.6 is not returning results. I’ll post again if I find a solution.
Ward
August 13th, 2008
For people having problems a couple of things
* turn off error reporting - error_reporting(0); in poll.php
* make sure to add write permissions on the directory and files that you are writing to in the flatfile application
* add ?> to the end of the poll.php file
* // quote these indexes in flatfile.php line 44 so they will stop triggering error notices
$comparison_type_for_col_type = array(
“INT_COL” => INTEGER_COMPARISON,
“DATE_COL” => INTEGER_COMPARISON, // assume Unix timestamps
“STRING_COL” => STRING_COMPARISON,
“FLOAT_COL” => NUMERIC_COMPARISON
);
If you are voting and the form fades out and nothing comes back it is likely contaminating the json form results response with error notices and the the js doesnt know what to do with the response - so turn off error reporting in poll.php. The rest of the problems appear to be with writing to the flatfile votes.txt, the ip recording works fine but no results are being recorded in the votes.txt. I’ll have to look at it some more.
Ward
August 14th, 2008
Ok, in poll.php also change line 41 from $id = $_GET['poll'] || $_POST['poll']; to $id = $_GET['vote'];
this logic is also on line 76 so I imagine it will probly fail there as well and should be changed. I am not sure why its written that way. Maybe in some circumstances the js switches from using a get to a post? It could be rewritten like this
isset($_POST['poll']) ? $id = $_POST['poll'] : $id = $_GET['poll'];
as a fallback in case of post vars, you will probly want to verify that post as well as get is working.
Joe
August 17th, 2008
Great script but my question is why someone would post a this polling solution without making sure it works!
rina
August 18th, 2008
cool! i like it! thanks!
Petter
August 20th, 2008
So bad PHP code I would say, that is for peoples for new to php?
I like the code but I’ve worked with PHP for a longer time now.
I would say that peoples that are new to PHP maybe not understand classes so good.
Kevin
August 20th, 2008
Has anyone been able to get the poll to increment in the votes.txt? Everything seems to be working well for me except the poll incrementing numbers.
hoverMN
August 21st, 2008
Same problem here ! please any body solve this…the vote is not registered….
ROTOR
August 29th, 2008
The vote is not counted…it remains same…
why such bothering…please make the script working….Many of us are facing same problem
Why don’t you reply…please