This post explains you about how to create PHP Cookie Ajax Based Poll Script.

This script is a complete ajax poll script and it is based on next previous buttons.In this script if a user submit a poll, we saved create a cookie with the current poll, so he can’t poll again.

Download Link   Demo Link

Database

This script is based on 2 tables, poll question and poll option.

Poll Question Table

CREATE TABLE IF NOT EXISTS `poll_question` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`question` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
)

Poll Option Table

CREATE TABLE IF NOT EXISTS `poll_option` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `poll_id` int(11) NOT NULL,
  `option` varchar(250) NOT NULL,
  `click` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ;

dbConnect.inc.php

Contains database connectivity code

$mysql_db_hostname = "Host name";
$mysql_db_user = "UserName";
$mysql_db_password = "Password";
$mysql_db_database = "Database Name";

$con = mysql_connect($mysql_db_hostname, $mysql_db_user,
       $mysql_db_password or die("Could not connect database");
//Create a new connection
mysql_select_db($mysql_db_database, $con) or
                 die("Could not select database");
// select database

All the ajax code is written in index.php.

Use submitPoll function for submit the poll.
Use loadPoll function for load the poll. loadPoll function contain 2 parameters, type and id and this type parameters define what you wana do, means u wana show the poll result or you wana show new poll.

index.php

<script type="text/javascript">
function submitPoll(id){
   $('#refresh').show();
   $('#refresh').fadeIn(400).html('Please Wait
       <img src="image/loading.gif" />');
   var optionId=$('input[name=pollOption]:radio:checked').val();
   if(optionId=='' || optionId==null){
      alert('Please select Option');
       $('#refresh').hide();
   }else{
      var type="addPoll";
      var dataString = 'id='+ optionId +'&type=' + type +'&qid=' + id;
      $.ajax({
          type: "POST",
          url: "prosess.php",
          data: dataString,
          cache: false,
          success: function(result){
               loadPoll('viewPoll',id);
          }
       });
   }
}

function loadPoll(type,id){
   $('#refresh').show();
   $('#refresh').fadeIn(400).html('Please Wait
       <img src="image/loading.gif" />');
   var dataString = 'id='+ id + '&type=' + type;
   $.ajax({
         type: "POST",
         url: "prosess.php",
         data: dataString,
         cache: false,
         success: function(result){
                $('#refresh').hide();
                $("#poll_wrap").html(result);
         }
    });
}

loadPoll('','');
</script>