Polling and Survey System in PHP, Mysql, AJAX

Abhigyan Singh 12th Jul 2020

This tutorial will explain about online polling or survey system in php, mysql and ajax. Commonly most of the website using polling system to get opinion about any topic. This polling system mainly used in News Website, Social website or any other web portals to get the user opinion.

Here you can get your viewer opinion on a specific topic. This system can be used as a survey system. Currently several website doing online survey. So this code is very useful for those who want to make their own polling system.

In this system, Site administrator just publishes a question with a set of answer and user just clicks on their desired answer. When he clicks the submit button, same time the overall answer result of this question will get publish.

Here, a user can give only one opinion or answer for a question. We are restricting user to give only one answer for one question. Here we are just restricting user by COOKIE. We are just set the COOKIE and in next submit we are just checking the value if it is avaliable, user cannot give second answer.

NOTE: You can restrict user by adding other method also like ask login for anser or other method.

$expire=time()+60*60*24*30;

setcookie("poll"."_".$setPollId, "poll"."_".$setPollId, $expire);

Now, Just create a table into your database for submiting the poll answer.

Note: This table is just storing the answer value. you can customise as per your need.

$expire=time()+60*60*24*30;
setcookie("poll"."_".$setPollId, "poll"."_".$setPollId, $expire);

Now, Just create a table into your database for submiting the poll answer.

Note: This table is just storing the answer value. you can customise as per your need.

CREATE TABLE IF NOT EXISTS `TABLE_NAME` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `poll_option` varchar(100) NOT NULL,
  `poll_count` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

HTML CODE HERE:

<div style="font: 15px tahoma; padding: 10px;width: 600px;">
<div>
<div style="padding:0 0 10px 0;">Which Programming Language is best for Web Development? </div>
<div id="pollDisplay">
<form>
<div style="padding:0 0 5px 0;">
<input type="radio" name="poll_option" id="1" class="poll_sys" value="1">
<label>JAVA</label>
</div>

<div style="padding:0 0 5px 0;">
<input type="radio" name="poll_option" id="2" class="poll_sys" value="2">
<label>PHP</label>
</div>

<div style="padding:0 0 5px 0;">
<input type="radio" name="poll_option" id="3" class="poll_sys" value="3">
<label>Asp .Net</label>
</div>

<div style="padding:0 0 10px 0;">
<input type="radio" name="poll_option" id="4" class="poll_sys" value="4">
<label>Others</label>
</div>

<input type="image" onclick="return submitPoll();" class="vote" src="submit.jpg" name="poll">
</form>
</div>
</div>
</div>

AJAX CODE HERE:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
function submitPoll()
{

var radios = $(".poll_sys");
var checked = '';
for (var i = 0; i < radios.length; i++) {
if (radios[i].checked) {
var checked = 'checked';
}}
if(checked == ''){
alert("Please select an Option to participate in the poll");
radios[0].focus();
return false;
}

var radiovalue= $('input[name="poll_option"]:checked').val();
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();

}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{

document.getElementById("pollDisplay").innerHTML=xmlhttp.responseText;
}

}

xmlhttp.open("GET","poll.php?vote="+radiovalue,true);
xmlhttp.send();
return false;
}
</script>

Here, there is one supporting js.

When user clicks on submit button then this ajax call happens and call poll.php file by passing the submitted value.

poll.php code here:

<?php
$dbConect = mysql_connect("localhost", "root", "");
mysql_select_db("", $dbConect);

$vote = $_REQUEST['vote'];

$setPollId = 2;

if(isset($_COOKIE['poll'."_".$setPollId]))
{
$msg = "You have already cast your vote.";
}

else
{
$query="update TABLE_NAME set poll_count=poll_count+1 where id='$vote'";
$sql = mysql_query($query);
$msg = "You have successfully cast your vote.";
}

?>

<?
$expire=time()+60*60*24*30;
setcookie("poll"."_".$setPollId, "poll"."_".$setPollId, $expire);
echo '<div class="poll_msg">'.$msg.'</div>';
?>

<div class="poll_y_n_c cf marginb5">
<?

$queryTotalPoll = "SELECT SUM(poll_count) as poll_count from TABLE_NAME";
$resultTotalPoll = mysql_query($queryTotalPoll);
$resultSetresultTotalPoll = mysql_fetch_array($resultTotalPoll);
$totalCount = $resultSetresultTotalPoll["poll_count"];

$queryDisplay = "SELECT poll_option, poll_count from TABLE_NAME";
$resultRec = mysql_query($queryDisplay);
while($resultSetRec = mysql_fetch_array($resultRec))
{
?>

<div class="poll_yes floatL paddingb5"><?=$resultSetRec['poll_option'];?></div>
<div class="paddingb5 floatL">
<img src="option.gif"
width='<?php echo(100*round($resultSetRec["poll_count"]/($totalCount),2)); ?>'
height='10'></div>
<div class="poll_percent floatR paddingb5"><?php echo(100*round($resultSetRec["poll_count"]/($totalCount),2)); ?>%</div>

<div class="cl"></div>
<?}?>

</div>

Authored By Abhigyan Singh

He is a continuous blogger and has blogged on different topic. He loves to surf Internet and always trying to get new Idea about new Technology and Innovations and sharing these great information to all the technology lovers.

ALSO ON DISCUSS DESK