Ajax Poll Script With PHP, MySQL & jQuery

In this tutorial, we'll be creating an Ajax Poll Script that displays the results with colored and animated lines using PHP, MySQL and jQuery.

The script has a pretty easy logic and can be implemented into any website quickly by simply calling a php function like getPoll(2) which brings the second poll.

Ajax Poll Script

Download

The code has 3 parts: HTML, JavaScript (jQuery) and PHP. Let's start with the easiest one:

HTML

The HTML for the poll is generated within the PHP function which is usually nice as it is only a 1-line-code and doesn't create a visual pollution in the overall HTML.

[sourcecode language=”html”]

Poll Question 1

  • Answer1 for Poll1
  •  
  • Answer2 for Poll1
  •  

Ajax Loader

[/sourcecode]

 

There is nothing original here. In one of the list items, we mention the answer and provide a unique ID for it which matches that answer's answerID in the database. For the other list item, we reserve it for the colored line, again by giving it the unique ID.

PHP

We first include our db connection file and handle the posted items from the Ajax requests

[sourcecode language=”php”]
require(“db.php”);
//GETTING VARIABLES START
if (isset($_POST[‘action’])) {
$action = mysql_real_escape_string($_POST[‘action’]);
}
if (isset($_POST[‘pollAnswerID’])) {
$pollAnswerID = mysql_real_escape_string($_POST[‘pollAnswerID’]);
}
//GETTING VARIABLES END
[/sourcecode]

After that, create the function getPoll which simply loops through the database and creates the HTML for the poll mentioned above.

[sourcecode language=”php”]
function getPoll($pollID){
$query = “SELECT * FROM polls LEFT JOIN pollAnswers ON polls.pollID = pollAnswers.pollID WHERE polls.pollID = ” . $pollID . ” ORDER By pollAnswerListing ASC”;
$result = mysql_query($query);
//echo $query;jquery

$pollStartHtml = ”;
$pollAnswersHtml = ”;

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$pollQuestion = $row[‘pollQuestion’];
$pollAnswerID = $row[‘pollAnswerID’];
$pollAnswerValue = $row[‘pollAnswerValue’];

if ($pollStartHtml == ”) {
$pollStartHtml = ‘

‘ . $pollQuestion .’

    ‘;
    $pollEndHtml = ‘

Ajax Loader

‘;
}
$pollAnswersHtml = $pollAnswersHtml . ‘

  • ‘ . $pollAnswerValue .’
  • ‘;
    $pollAnswersHtml = $pollAnswersHtml . ‘

  • ‘;
    }
    echo $pollStartHtml . $pollAnswersHtml . $pollEndHtml;
    }
    [/sourcecode]

     

    This getPollID function below is for easily getting the ID of the related polls by providing an answerID:

    [sourcecode language=”php”]
    function getPollID($pollAnswerID){
    $query = “SELECT pollID FROM pollAnswers WHERE pollAnswerID = “.$pollAnswerID.” LIMIT 1″;
    $result = mysql_query($query);
    $row = mysql_fetch_array($result);

    return $row[‘pollID’];
    }
    [/sourcecode]

    And, the getPollResults function which is kinda tricky but not that much.

    When ran, it returns a string like: 1|13|#ffcc00-2|32|#00ff00-3|18|#cc0000-63" which the first number is the answerID, second is the points it has and third is the color for that answer's animated line.

    The last number is the sum of all points for easily calculating percentages.

    The string is parsed in the JavaScript side later on.

    [sourcecode language=”php”]
    function getPollResults($pollID){
    $colorArray = array(1 => “#ffcc00”, “#00ff00”, “#cc0000”, “#0066cc”, “#ff0099”, “#ffcc00”, “#00ff00”, “#cc0000”, “#0066cc”, “#ff0099”);
    $colorCounter = 1;
    $query = “SELECT pollAnswerID, pollAnswerPoints FROM pollAnswers WHERE pollID = “.$pollID.””;
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result))
    {
    if ($pollResults == “”) {
    $pollResults = $row[‘pollAnswerID’] . “|” . $row[‘pollAnswerPoints’] . “|” . $colorArray[$colorCounter];
    } else {
    $pollResults = $pollResults . “-” . $row[‘pollAnswerID’] . “|” . $row[‘pollAnswerPoints’] . “|” . $colorArray[$colorCounter];
    }
    $colorCounter = $colorCounter + 1;
    }
    $query = “SELECT SUM(pollAnswerPoints) FROM pollAnswers WHERE pollID = “.$pollID.””;
    $result = mysql_query($query);
    $row = mysql_fetch_array( $result );
    $pollResults = $pollResults . “-” . $row[‘SUM(pollAnswerPoints)’];
    echo $pollResults;
    }
    [/sourcecode]

    The last PHP code is the voting part which also sets a cookie:

    [sourcecode language=”php”]
    //VOTE START
    if ($action == “vote”){

    if (isset($_COOKIE[“poll” . getPollID($pollAnswerID)])) {
    echo “voted”;
    } else {
    $query = “UPDATE pollAnswers SET pollAnswerPoints = pollAnswerPoints + 1 WHERE pollAnswerID = “.$pollAnswerID.””;
    mysql_query($query) or die(‘Error, insert query failed’);
    setcookie(“poll” . getPollID($pollAnswerID), 1, time()+259200, “/”, “.webresourcesdepot.com”);
    getPollResults(1);
    }
    }
    //VOTE END

    if (mysql_real_escape_string($_GET[‘cleanCookie’]) == 1){
    setcookie(“poll1”, “”, time()-3600, “/”, “.webresourcesdepot.com”);
    header(‘Location: http://webresources.wpengine.com/wp-content/uploads/file/ajax-poll-script/’);
    }
    [/sourcecode]

    jQuery (JavaScript)

    It is only an Ajax request that posts the selected answer to the PHP code, gets the string in return, parses it and makes some show/hide tricks for displaying and hiding the messages or loaders.

    Here it is:

    [sourcecode language=”javascript”]
    $(document).ready(function() {

    $(“#pollAjaxLoader”).hide(); //hide the ajax loader
    $(“#pollMessage”).hide(); //hide the ajax loader
    $(“#pollSubmit”).click(function() {
    var pollAnswerVal = $(‘input:radio[name=pollAnswerID]:checked’).val();//Getting the value of a selected radio element.
    if ($(‘input:radio[name=pollAnswerID]:checked’).length) {
    $(“#pollAjaxLoader”).show(); //show the ajax loader
    $.ajax({
    type: “POST”,
    url: “inc/functions.php”,
    data: { pollAnswerID: pollAnswerVal, action: “vote” },
    success: function(theResponse) {
    //the functions.php returns a response like “1|13|#ffcc00-2|32|#00ff00-3|18|#cc0000-63” which the first number is the answerID, second is the points it has and third is the color for that answer’s graph. The last number is the sum of all points for easilt calculating percentages.
    if (theResponse == “voted”) {
    $(“#pollAjaxLoader”).hide(); //hide the ajax loader
    $(“#pollMessage”).html(“sorry, you already voted.”).fadeTo(“slow”, 1);
    } else {
    var numberOfAnswers = (theResponse).split(“-“).length-2;//calculate the number of answers
    var splittedResponse = (theResponse).split(“-“);
    var pollAnswerTotalPoints = splittedResponse[numberOfAnswers+1];

    for (i=0;i<=numberOfAnswers;i++) { var splittedAnswer = (splittedResponse[i]).split("|"); var pollAnswerID = (splittedAnswer[0]); var pollAnswerPoints = (splittedAnswer[1]); var pollAnswerColor = (splittedAnswer[2]); var pollPercentage = (100 * pollAnswerPoints / pollAnswerTotalPoints); $(".pollChart" + pollAnswerID).css("background-color",pollAnswerColor); $(".pollChart" + pollAnswerID).animate({width:pollPercentage + "%"}); $("#pollAnswer" + pollAnswerID).html(" (" + Math.round(pollPercentage) + "% - " + pollAnswerPoints + " votes)"); $("#pollRadioButton" + pollAnswerID).attr("disabled", "disabled"); //disable the radio buttons } $("#pollAjaxLoader").hide(); //hide the ajax loader again $("#pollSubmit").attr("disabled", "disabled"); //disable the submit button } } }); return false; } else { $("#pollMessage").html("please select an answer.").fadeTo("slow", 1, function(){ setTimeout(function() { $("#pollMessage").fadeOut("slow"); }, 3000); }); return false; } }); }); [/sourcecode]

    The JavaScript code is well-commented and is pretty self-explanatory. Don't let the number of lines afraid you, most of them are just the make-up.

    That's all.

    • http://csscollector.com Css Collector

      I like it!
      Thanks!

    • peter

      Does nobody still not code in mysqli or pdo these days

    • http://www.popcorndesign.gr Popcorn WebDesign

      Great share, thank you!

    • David

      Very cute!

    • Nacho

      Great! Thanks

    • http://1click.at Michael Pehl

      Nice work. Thank you for sharing :)

    • http://www.rouse.ws William Rouse

      Can you show me a method to clear the cookie in my development version of this tutorial. I tried using a submit button to run a PHP page with the following line, but it did not work.
      setcookie(“poll” . getPollID($pollAnswerID), 1, time() -259210, “/”);

      Thanks!
      WBR

    • http://www.webresourcesdepot.com Umut M.

      @William,

      Your code looks ok and I think it fails in the “/” part.

      Suggest you use the domain while setting the cookie at the voting process like:

      setcookie(“poll” . getPollID($pollAnswerID), 1, time()+259200, “/”, “.domain.com”);

      setcookie(“poll” . getPollID($pollAnswerID), 1, time()-259200, “/”, “.domain.com”);

      To make sure, you can check the cookie in your browsers “view cookies” part and verify if there is a mismatch.

    • http://sionvalais.com/poll/ Maek

      The javascript part is not working correctly. Is there something wrong?

    • http://www.webresourcesdepot.com Umut M.

      @Maek,

      Whn I check it with Firebug, the eror below is generated in your eample:

      ***
      Cannot modify header information – headers already sent by
      ***

      which is generally a whitespace problem.

      Can you make sure that there is no whitespace in the functions.php and db.php files.

      More about the error: http://www.geeklog.net/faqman/index.php?op=view&t=38

    • Dominic

      hi there, When i install, it comes up with this error, can someone tell me what im doing wrong. I checked everything, and i followed the install instructions.

      Heres the error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/scenepr1/public_html/domains/inspiredagency.com/poll/inc/functions.php on line 14

      thanks

    • Vlada

      Whether it can be set to a visitor can vote every 24 hours after?

    • Vlada

      and if you can stand next to the button and see the vote result. If all this can then be extra poll

    • http://www.radyoender.com Ender

      tnx

    • Elaine Blakeman

      Can this code be modified to make a poll with more than one question? Also, can you do open-ended questions and multiple choice questions? That is one thing I cannot find on the web — an equivilent to the excellent Adobe Connect Polling feature…

    • Balint Istvan

      Hy. How i make if i vote once, next time i refresh the page to show the vote results only? Thx

    • http://www.kamilalibekov.com Kamil

      to Dominic and everyone who has mysql “warning..line14”

      that error came from your MySQL
      99% that you have pollanswers instead of pollAnswers (note capitalized letter A).. or you can properly rename sql queries in your php (if you don’t have direct access t your db).
      Anyways, I’m suggesting to rename that table to pollAnswers and everything will work like a charm.

    • Jean

      How to get the results of the poll even if you have not voted?

    • http://www.webresourcesdepot.com Umut M.

      @Kamil,
      Thanks for the info on that.

      @Jean,
      The feature is not included by default but you can easily extend the code for that.

    • Christian

      First at all apologise my poor english. Im from Argentina.
      Testing the poll i have one error. I have to make a poll with 4 categories, so created on DB table pull the 4 categories with diferent pollQuestion autoincremente pollID and pollStatus 1 for all. The problem appears when on index.php i put this:

      The first question or categorie work ok but the others 2 showme the loader and when i click on vote send me to

      http://localhost/………./ajax-poll/inc/functions.php?action=vote

      and didnt showme any results.

      Excuse me again, and TKS a lot!!!

    • Christian

      index.php i put this:

      either with 2,3 or 4, example:

      but works ok like this:

      TKS!!

    • Kaspars

      Notice: Undefined index: action in G:\wamp\www4\polls\functions.php on line 5

      Notice: Undefined index: pollAnswerID in G:\wamp\www4\polls\functions.php on line 6

      and

      Notice: Undefined variable: pollStartphp in G:\wamp\www4\polls\functions.php on line 21

      Notice: Undefined variable: pollAnswersHtml in G:\wamp\www4\polls\functions.php on line 25

      Notice: Undefined variable: pollStartphp in G:\wamp\www4\polls\functions.php on line

    • Al

      Hi and thanks for the poll!
      I got it working after changing pollAnswers to pollanswers, but now I would like to have multiple polls on 1 page. When I do this though, all polls stop working, and ajaxloader.gif is visible on the second poll.

      Is there an easy way to fix this?
      Thanks!

    • mojtaba

      i just can say
      great job!

    • ryan

      @kamil,
      thx, but still getting the error:

      Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource on line 14

      what permissions should i have on the server files?

      i changed all capital A to lowercase a – in the actual files, and left the mysql stuff alone.

      any thing else i could be missing?

      thx – r

    • Duran

      Hy. How i make if i vote once, next time i refresh the page to show the vote results only? Thx

    • http://www.ttyazilim.com taner

      Hy. How i make if i vote once, next time i refresh the page to show the vote results only? Thx

    • http://www.webresourcesdepot.com Umut M.

      @Taner,

      I suggest implementing a cookie or IP-based control mechanism.

    • Ansu

      hey this scripts works greatly.but i need to see the result even if i havenot voted can u help me please

    • http://callmestupidbut.co.uk Heather

      Is there a way to integrate this into a WordPress post? I’ve got it working in the sidebar, but I can’t call the function in a post.
      Thanks in advance

    • http://www.jelbee.tk Streaming Video

      I love this button: ” clear the cookie to vote again” :)

    • m_prof

      i got this errors when run it any body can resolve it
      i work in xamp

      Notice: Undefined index: action in C:\xampp\htdocs\xx\ajax-poll\inc\functions.php on line 4

      Notice: Undefined index: pollAnswerID in C:\xampp\htdocs\xx\ajax-poll\inc\functions.php on line 5

      Notice: Undefined variable: pollStartHtml in C:\xampp\htdocs\xx\ajax-poll\inc\functions.php on line 20

      Notice: Undefined variable: pollAnswersHtml in C:\xampp\htdocs\xx\ajax-poll\inc\functions.php on line 24

    • sujan

      i want to create link to ‘View Results’ beside Vote button. what would be the url for View Results ? Can anyone help? I need that badly :-(

    • http://www.webresourcesdepot.com Umut M.

      @sujan,

      That’s very easy but I hope you are a little familiar with JS and PHP.

      Just add this at the end of inc/functions.php:
      if ($action == “getResults”){
      getPollResults(1);
      }
      }

      Insert the JS below to the js/poll.js just before the last “});”:

      $(“.showResults”).click(function() {
      $(“#pollAjaxLoader”).show(); //show the ajax loader
      $.ajax({
      type: “POST”,
      url: “inc/functions.php”,
      data: { pollAnswerID: pollAnswerVal, action: “getResults” },
      success: function(theResponse) {

      var numberOfAnswers = (theResponse).split(“-“).length-2;//calculate the number of answers
      var splittedResponse = (theResponse).split(“-“);
      var pollAnswerTotalPoints = splittedResponse[numberOfAnswers+1];

      for (i=0;i<=numberOfAnswers;i++) { var splittedAnswer = (splittedResponse[i]).split("|"); var pollAnswerID = (splittedAnswer[0]); var pollAnswerPoints = (splittedAnswer[1]); var pollAnswerColor = (splittedAnswer[2]); var pollPercentage = (100 * pollAnswerPoints / pollAnswerTotalPoints); $(".pollChart" + pollAnswerID).css("background-color",pollAnswerColor); $(".pollChart" + pollAnswerID).animate({width:pollPercentage + "%"}); $("#pollAnswer" + pollAnswerID).html(" (" + Math.round(pollPercentage) + "% - " + pollAnswerPoints + " votes)"); $("#pollRadioButton" + pollAnswerID).attr("disabled", "disabled"); //disable the radio buttons } $("#pollAjaxLoader").hide(); //hide the ajax loader again $("#pollSubmit").attr("disabled", "disabled"); //disable the submit button } }); return false; }); And give the class "showResults" to the HTML element that wraps your "view results" text. There can be typos in the code I wrote but simply, it only asks for the poll results and parses just the same way it does when someone votes. This could have been done in a much smarter way without duplicating the code but this is a quick workaround.

    • http://None Alaa Ebrahim

      Hi, Thx alot for this poll script :)
      i got aproblem ..
      when i press on submit button it became “disabled” >> that means that all thing is right .. But no results shown !!
      i hope to help in this

    • http://www.webresourcesdepot.com Umut M.

      @Alaa,

      I suggets checking it with Firebug to find out if any errors are generated.

    • http://sujanshakya.com.np sujan

      thanks a lot :-) :-)

    • http://sujanshakya.com.np sujan

      “And give the class “showResults” to the HTML element that wraps your “view results” text.”

      can u give me one more example for this one?
      a href=”???????” class=”showResults”>View Results</a

    • Delano

      You might also be able to post the database?
      Thanks.

    • http://www.webresourcesdepot.com Umut M.

      @@Delano,

      It is included in the download package.

    • sujan shakya

      i still could not get result of poll without voting.

      how can i show poll result without voting? I tried all above codes but still …. :-(.

      please do help.

      thank you !

    • Julio

      Hi, I’m getting these error, Do you know, What can be?

      Notice: Undefined index: action in C:\wamp\www\concurso\poll\inc\functions.php on line 4

      Notice: Undefined index: pollAnswerID in C:\wamp\www\concurso\poll\inc\functions.php on line 5

      Notice: Undefined variable: pollStartHtml in C:\wamp\www\concurso\poll\inc\functions.php on line 20

      Notice: Undefined variable: pollAnswersHtml in C:\wamp\www\concurso\poll\inc\functions.php on line 24

      BR

    • http://www.webresourcesdepot.com Umut M.

      @Julio,

      Updates the download file which should now be working fine (only updated the functions.php file).

    • Anders

      Where can I find the SQL tables?
      I cant find it ….???
      I’ve tried to make my own, but i cant…

    • http://www.webresourcesdepot.com Umut M.

      @Anders,

      Seems like I didn’t add it after the last update. Fixed now.

    • GM Revan

      I think I found a bug. If you press vote really fast it votes several times. Checked it in my website too and the same thing happens. So I changed some code in poll.js .I moved the ” $(“#pollSubmit”).attr(“disabled”, “disabled”);”

      to just below $(“#pollSubmit”).click(function()
      { $(“#pollSubmit”).attr(“disabled”, “disabled”);

      and now works fine.

    • GM Revan

      Btw can I check someones IP instead of a cookie when he clicks vote. How can I do that in this poll ?

      thanks!

    • GM Revan

      Damm didn’t think about not selecting something and pressing click. So i guess the problems persist.

    • GM Revan

      Ok fixed it for good.Now you can only vote once and no mater how fast you press the vote button and if you don’t select anything the button is no longer disabled.The correct code was:

      change to the poll.js

      I moved the : “$(“#pollSubmit”).attr(“disabled”,“disabled”);”

      As seen just below:

      if ($(‘input:radio[name=pollAnswerID]:checked’).length) {
      $(“#pollSubmit”).attr(“disabled”, “disabled”);
      $.ajax({

      Now the only problem i have with the poll is is it can vote based on ip and its perfect.

    • hamid

      hi dear friend
      thank you for nice code.i have suggestion.could you put MySQL part code(like tables)in your download package.i think it would be really useful

    • http://www.vinde.eu/poll/ Carsten

      Nice poll!
      However I get this error:
      “Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /customers/vinde.eu/vinde.eu/httpd.www/poll/inc/functions.php on line 22”

      Can anyone help me?

    • Kathy Bee

      Great tutorial and script, thank you for taking the time to put this together. I got this to work easily with pollID1 but I can’t seem to get other pollIDs to return results. I would like to be able to have multiple polls on one page. Is this possible?

    • valdi

      The guide is not good for people which cannot html and php. I cannot this install.

    • http://eltonkz.co.cc ska

      nice poll
      but i get error
      Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/a2353437/public_html/ajax-poll/inc/functions.php on line 22
      anyone can help me??

    • bleng

      how to set 24 hours cookie. Its mean users can vote each day one poll. Its possible ….?

    • http://www.webresourcesdepot.com Umut M.

      @bleng,

      Yes, definitely. I suggest checking: http://www.w3schools.com/php/php_cookies.asp

    • bleng

      i tried this poll on http://poll.comze.com/ . But ihave problem.

      Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/a1515724/public_html/inc/functions.php on line 22

      how i fix this issue

    • Kevin

      Thanks for the script. I have a problem. Everything works but not the results. When you vote it wont show the results. (testing on Mozilla v.3.6.25).

    • http://gameshow12h.vn TieuKaKa

      @Kevin

      “Thanks for the script. I have a problem. Everything works but not the results. When you vote it wont show the results. (testing on Mozilla v.3.6.25).”

      1> First : function getPollResults($pollID){
      $pollResults = ”;

      2> Second: change if ($pollResults == ” “) to if ($pollResults == ”)

      So that you’ll fix this

    • ggg

      thank you for Ajax Poll Script

    • Kevin

      unfortunately the results fix didnt work :(

    • TSC

      Getting errors:

      Notice: Undefined variable: action in web/content/test/poll/inc/functions.php on line 69

      Notice: Undefined index: cleanCookie in /web/content/test/poll/inc/functions.php on line 82

      Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /web/content/test/poll/inc/functions.php on line 22

      Notice: Undefined variable: pollEndHtml in /web/content/test/poll/inc/functions.php on line 35

    • PD

      I got error too..Please help.

      Notice: Undefined variable: action in web/content/test/poll/inc/functions.php on line 69

      Notice: Undefined index: cleanCookie in /web/content/test/poll/inc/functions.php on line 82

    • aaron

      How can you change this to have only two (rather than three) poll options?

    • http://www.webresourcesdepot.com Umut M.

      @aaron,

      The options are stored in the database. You can add any number of options.

    • aaron

      The pool works great (thank you btw) when I have just one poll. But It doesn’t seem to work when I create a second poll. I think It might be just mysql stuff when I added a new poll. Does this look right to create another poll with two possible answers:

      — Do these two create the two possible answers ?

      INSERT INTO `pollAnswers` VALUES (‘1’, ‘2’, ‘Green Light’, ‘1’, ‘1’);
      INSERT INTO `pollAnswers` VALUES (‘2’, ‘2’, ‘Cancel’, ‘1’, ‘1’);

      — Does this create the second poll?

      INSERT INTO `polls` VALUES (‘2’, ‘Poll Question 2’, ‘1’);

      Can you clarify what pollAnswerValue, pollAnswerPoints, pollAnswerListing

      Thanks again for the tutorial

    • aaron

      sorry poll (not pool)

    • Evildark

      Notice: Undefined index: action in C:\wamp\www\ajax-poll\inc\functions.php on line 69

      Notice: Undefined index: cleanCookie in C:\wamp\www\ajax-poll\inc\functions.php on line 82

      What should I do to fix this? Thanks.

    • mooho

      i got error too

      Notice: Undefined variable: action in C:\xampp\htdocs\ajax-poll\inc\functions.php on line 69

      Notice: Undefined index: cleanCookie in C:\xampp\htdocs\ajax-poll\inc\functions.php on line 82

    • Shalin

      I am stuck into this..

      I am getting error as “uncaught exception: [Exception… “Could not convert JavaScript argument arg 0 [nsIDOMWindow.getComputedStyle]” nsresult: “0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)” location: “JS frame :: http://localhost:8080/ajax-poll/inc/js/jquery-1.4.2.min.js :: :: line 4″ data: no]
      http://localhost:8080/ajax-poll/inc/js/jquery-1.4.2.min.js
      Line 2″

      Please help me soon…..the options results are shown as NAN% also

    • allister

      Shalin:
      try changing db table name from

      pollAnswers

      to

      pollanswers

      in your functions.php file. There are several occurrences, remember to change them all.

      I also found out that there is a hardcoded poll id in functions.php line 77 so change

      getPollResults(1);

      to

      getPollResults(getPollID($pollAnswerID));

    • Greysonn

      I’m trying to submit the vote as soon as people press the radio button by adding;
      onClick=”document.getElementById(‘pollSubmit’).click();”
      to the radio button html element in functions.php
      but apparently that gives an unexpected T_STRING on line 32 in functions.php which atm consists of:

      $pollAnswersHtml = $pollAnswersHtml . ‘ ‘ . $pollAnswerValue .”;

      I’ve tried everything within my understanding and its driving me insane :/ any suggestions?

      Thanks in advance

    • Greysonn

      Awesome the copy paste failed, no surprise there,
      this pic should give a better view; the third line is line 32
      http://i.imgur.com/EhW1E.png

    • Greysonn

      Well what dyou know;
      onClick=”document.getElementById(\’pollSubmit\’).click();”
      that should work;

      tho now it opens URL/scripts/poll/inc/functions.php?action=vote as a new page
      any ideas?

      Thanks in advance

    • Greysonn

      Woot it works ^^
      as previously suggested by someone i moved the disabling of the submit button line much higher up, tho for some reason also placed the disabling of the radio buttons over there, and that was not a good idea x]

    • nate

      Having trouble. I installed my database, and i got this error message.

      How can i solve this/?

      Thanks.

      while($row = mysql_fetch_array($result, MYSQL_ASSOC))

    • http://www.instant-pyrolysis.com Valenten

      Very nice, and funny how close it was to the one I developed just before visiting this website ^^

    • Thiago

      Although he made ​​the suggestion for a button to display the results, I could not follow your explanation for a comment above. Could assist in the implementation of this button to display the results? Sorry for English. I am Brazilian. Thank you.

    • Orly

      Hi,

      I’ve installed the files, but I get an error:
      Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /public_html/poll/inc/functions.php on line 22

      can anybody help?

      thanks

    • bharanikumar

      some one please update table schema for poll.

    • Hidayet Karazor

      Hi. I converted this great script from PHP to Classical ASP. Everything is Ok. The script works well but I want to ask that how can we modify this script for showing the voted poll results. Namely, When someone voted this script then came to site again and want to vote the same poll again but in this version there is only a warning such as “You have voted this poll before.” but with this warning can we load the show results with the standart animate function?

      I’m not an expert of Jquery or Php but i try to do something. i need your experience, please give me some advice if possible a working code maybe:))

    • yasmin

      i have problems trying to add more polls in the same page. anyone knows how to solve this problem, because it doesnt work when you put more polls, the #pollAjaxLoader still show and when i push vote shows a blank page.

    • http://help yasmin

      anyone knows how to put more polls in the same page and work?. because i tried but it doesnt work. i think i have to change the code but i dont know how, because the forms and input names in the two or three polls are the same so i don’t know how to modified the code. thanks

    • abcd

      HI,

      I also faced the notice errors but i resolved by declaring the $action=”; and $_GET[‘cleancookie’]=”;

      Now when i click on vote,
      Following functinality performs well:
      1)A session gets created,
      2) User is not allowed to vote more than once
      3)the selection is entered in the db.

      But when you click on vote results are not displayed, it shows loading :(

      Plz help.

    • varun

      poll is perfect but there must be form for adding new question and answer after that we just need to change poll id and i also can’t find the way to change poll question

    • Ola

      Problem:
      When I press ‘Submit’ button, vote insert nicely in mysql. But the result page not shows. The voting page remain active and a revolving loading gif image shows next to submit button.

    • Ola

      After hard work, now its working.
      But cookie not setting up. So I can vote unlimited.

      Please tell me how to solve this, I mean how cookie will set up and how can I setup cookie for 7 days.

    • elkhadir

      when there is declaration of $pollResults??

    • mitchka

      Hello
      How can I add the fields name and email ? I’ve added it in the php file but I don’t know how to add it in the Ajax file
      Thanks a lot

    • http://www.atharmarketing.net Afaq

      Hi. I just want to say that i use this script locally, its working fine but as i upload it on online server, it display me empty page, means its not getting form from functions.php.
      Kindly reply me that how to resolve this issue?

    • Anree

      WTF?

      Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/s/stazarov/autoprofi.com/public_html/newyear2013/poll/ajax-poll/inc/functions.php on line 22

    • AFAQ AHMED

      Hi. I just want to say that i use this script locally, its working fine but as i upload it on online server, it display me empty page, means its not getting form from functions.php.
      Kindly reply me that how to resolve this issue?

      YOU CAN SEE ITS EXAMPLE LIVE.

      http://www.pacecircle.com/polls

    • aapurnama

      Hello,

      what must i do to fix this:

      ( ! ) Notice: Undefined variable: action in E:\wamp\www\ajax-poll-script\ajax-poll\inc\functions.php on line 70
      Call Stack
      # Time Memory Function Location
      1 0.0004 673000 {main}( ) ..\index.php:0
      2 0.0045 722832 require( ‘E:\wamp\www\ajax-poll-script\ajax-poll\inc\functions.php’ ) ..\index.php:3

      ( ! ) Notice: Undefined index: cleanCookie in E:\wamp\www\ajax-poll-script\ajax-poll\inc\functions.php on line 83
      Call Stack
      # Time Memory Function Location
      1 0.0004 673000 {main}( ) ..\index.php:0
      2 0.0045 722832 require( ‘E:\wamp\www\ajax-poll-script\ajax-poll\inc\functions.php’ ) ..\index.php:3

      thaks for your care

    • AFAQ AHMED

      is not working. Form not displaying.

      http://www.pacecircle.com/polls

    • http://danielrezendepessoa.com Daniel Rezende Pessoa

      For everyone who has mysql “Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/s/stazarov/autoprofi.com/public_html/newyear2013/poll/ajax-poll/inc/functions.php on line 22”, this error came from your connection to MySQL in $dbname. At in db.php change $dbname = “polls”; to $dbname = “your database name”
      And, don’t forget to rename the table from “pollanswers” to “pollAnswers” and everything will works fine.

    • manish chandra

      Hi,

      While running code after download i am getting this error:-
      Notice: Undefined index: cleanCookie in D:\wamp\www\ajax-poll\inc\functions.php on line 82
      Notice: Undefined variable: action in D:\wamp\www\ajax-poll\inc\functions.php on line 69

      and after select option and click on voter button ajax loader cont.. running and not showing results.

      Thanks.
      Manish

    • Squizer

      BTW I can’t find MySql table. And I can’t create it. where can i find it? Or Could you please include it in resource.

    • roberto

      Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /web/htdocs/www.ultraspettacoli.com/home/ajax-poll/inc/functions.php on line 22

      what can i do?

    • roberto

      i use
      $query = mysql_query($sql_query) or die(mysql_error());

      4
      and the reation is

      Query was empty

    Search