Ajax Poll Script With PHP, MySQL & jQuery

11°

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.

    99 Comments
    1. Reply Css Collector June 22, 2010 at 9:11 AM

      I like it!
      Thanks!

    2. Reply peter June 22, 2010 at 11:52 AM

      Does nobody still not code in mysqli or pdo these days

    3. Reply Popcorn WebDesign June 22, 2010 at 12:57 PM

      Great share, thank you!

    4. Reply David June 23, 2010 at 4:45 AM

      Very cute!

    5. Reply Nacho June 23, 2010 at 4:59 AM

      Great! Thanks

    6. Reply Michael Pehl June 25, 2010 at 4:13 AM

      Nice work. Thank you for sharing 🙂

    7. Reply William Rouse June 25, 2010 at 3:07 PM

      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

    8. Reply Umut M. June 25, 2010 at 5:40 PM

      @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.

    9. Reply Maek June 26, 2010 at 5:47 AM

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

    10. Reply Umut M. June 26, 2010 at 9:44 AM

      @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

    11. Reply Dominic June 27, 2010 at 5:20 AM

      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

    12. Reply Vlada July 21, 2010 at 2:42 PM

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

    13. Reply Vlada July 21, 2010 at 2:45 PM

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

    14. Reply Ender August 1, 2010 at 3:18 PM

      tnx

    15. Reply Elaine Blakeman August 4, 2010 at 11:56 AM

      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…

    16. Reply Balint Istvan August 10, 2010 at 3:05 AM

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

    17. Reply Kamil August 10, 2010 at 5:50 AM

      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.

    18. Reply Jean August 15, 2010 at 1:34 PM

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

    19. Reply Umut M. August 15, 2010 at 7:29 PM

      @Kamil,
      Thanks for the info on that.

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

    20. Reply Christian November 26, 2010 at 3:14 PM

      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!!!

    21. Reply Christian November 26, 2010 at 3:22 PM

      index.php i put this:

      either with 2,3 or 4, example:

      but works ok like this:

      TKS!!

    22. Reply Kaspars December 8, 2010 at 6:12 PM

      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

    23. Reply Al December 9, 2010 at 4:35 AM

      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!

    24. Reply mojtaba March 14, 2011 at 3:16 AM

      i just can say
      great job!

    25. Reply ryan March 16, 2011 at 4:58 PM

      @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

    26. Reply Duran May 16, 2011 at 5:21 AM

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

    27. Reply taner May 16, 2011 at 4:39 PM

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

    28. Reply Umut M. May 17, 2011 at 1:59 AM

      @Taner,

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

    29. Reply Ansu May 17, 2011 at 3:51 AM

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

    30. Reply Heather May 24, 2011 at 8:51 AM

      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

    31. Reply Streaming Video June 12, 2011 at 2:49 PM

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

    32. Reply m_prof June 22, 2011 at 3:03 AM

      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

    33. Reply sujan July 6, 2011 at 1:04 AM

      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 🙁

    34. Reply Umut M. July 6, 2011 at 12:14 PM

      @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.

    35. Reply Alaa Ebrahim July 11, 2011 at 4:03 PM

      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

    36. Reply Umut M. July 12, 2011 at 12:16 AM

      @Alaa,

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

    37. Reply sujan July 13, 2011 at 9:05 PM

      thanks a lot 🙂 🙂

    38. Reply sujan July 13, 2011 at 9:42 PM

      “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

    39. Reply Delano August 7, 2011 at 3:18 PM

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

    40. Reply Umut M. August 9, 2011 at 2:26 PM

      @@Delano,

      It is included in the download package.

    41. Reply sujan shakya August 13, 2011 at 10:51 PM

      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 !

    42. Reply Julio August 24, 2011 at 12:18 PM

      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

    43. Reply Umut M. August 25, 2011 at 8:31 AM

      @Julio,

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

    44. Reply Anders August 30, 2011 at 8:22 AM

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

    45. Reply Umut M. August 30, 2011 at 5:08 PM

      @Anders,

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

    46. Reply GM Revan September 8, 2011 at 9:43 AM

      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.

    47. Reply GM Revan September 8, 2011 at 9:57 AM

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

      thanks!

    48. Reply GM Revan September 8, 2011 at 2:26 PM

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

    49. Reply GM Revan September 10, 2011 at 8:48 AM

      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.

    50. Reply hamid September 18, 2011 at 4:02 PM

      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

    51. Reply Carsten September 19, 2011 at 2:30 PM

      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?

    52. Reply Kathy Bee September 23, 2011 at 5:01 PM

      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?

    53. Reply valdi October 12, 2011 at 2:32 AM

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

    54. Reply ska November 3, 2011 at 9:46 PM

      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??

    55. Reply bleng November 23, 2011 at 5:15 AM

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

    56. Reply Umut M. November 24, 2011 at 6:03 AM

      @bleng,

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

    57. Reply bleng November 24, 2011 at 11:59 AM

      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

    58. Reply Kevin January 5, 2012 at 5:55 AM

      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).

    59. Reply TieuKaKa January 9, 2012 at 12:38 PM

      @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

    60. Reply ggg January 16, 2012 at 11:57 PM

      thank you for Ajax Poll Script

    61. Reply Kevin January 17, 2012 at 7:39 AM

      unfortunately the results fix didnt work 🙁

    62. Reply TSC January 21, 2012 at 1:53 PM

      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

    63. Reply PD January 25, 2012 at 8:31 AM

      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

    64. Reply aaron January 26, 2012 at 7:24 PM

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

    65. Reply Umut M. January 29, 2012 at 8:14 AM

      @aaron,

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

    66. Reply aaron February 3, 2012 at 10:08 AM

      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

    67. Reply aaron February 3, 2012 at 10:08 AM

      sorry poll (not pool)

    68. Reply Evildark February 3, 2012 at 12:54 PM

      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.

    69. Reply mooho February 4, 2012 at 3:44 AM

      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

    70. Reply Shalin February 7, 2012 at 2:47 AM

      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

    71. Reply allister February 29, 2012 at 9:52 AM

      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));

    72. Reply Greysonn April 11, 2012 at 6:04 AM

      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

    73. Reply Greysonn April 11, 2012 at 6:05 AM

      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

    74. Reply Greysonn April 11, 2012 at 6:44 AM

      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

    75. Reply Greysonn April 11, 2012 at 7:03 AM

      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]

    76. Reply nate July 2, 2012 at 2:24 PM

      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))

    77. Reply Valenten July 5, 2012 at 7:11 AM

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

    78. Reply Thiago July 8, 2012 at 10:12 AM

      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.

    79. Reply Orly July 17, 2012 at 9:53 AM

      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

    80. Reply bharanikumar July 26, 2012 at 12:25 PM

      some one please update table schema for poll.

    81. Reply Hidayet Karazor August 31, 2012 at 2:35 PM

      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:))

    82. Reply yasmin September 11, 2012 at 1:58 PM

      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.

    83. Reply yasmin September 11, 2012 at 2:28 PM

      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

    84. Reply abcd October 24, 2012 at 6:45 AM

      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.

    85. Reply varun November 5, 2012 at 1:43 AM

      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

    86. Reply Ola November 12, 2012 at 12:31 AM

      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.

    87. Reply Ola November 12, 2012 at 8:25 AM

      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.

    88. Reply elkhadir November 16, 2012 at 5:18 PM

      when there is declaration of $pollResults??

    89. Reply mitchka November 23, 2012 at 10:05 AM

      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

    90. Reply Afaq December 19, 2012 at 5:36 AM

      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?

    91. Reply Anree December 21, 2012 at 7:32 AM

      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

    92. Reply AFAQ AHMED December 23, 2012 at 1:27 PM

      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

    93. Reply aapurnama December 27, 2012 at 11:41 PM

      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

    94. Reply AFAQ AHMED December 28, 2012 at 6:50 AM

      is not working. Form not displaying.

      http://www.pacecircle.com/polls

    95. Reply Daniel Rezende Pessoa January 10, 2013 at 7:27 AM

      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.

    96. Reply manish chandra May 1, 2013 at 12:06 AM

      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

    97. Reply Squizer July 25, 2013 at 3:07 AM

      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.

    98. Reply roberto August 30, 2013 at 11:11 AM

      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?

    99. Reply roberto August 30, 2013 at 11:23 AM

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

      4
      and the reation is

      Query was empty

    Leave a reply

    Search