Want To Be A Blogger  →  Start A Blog  →  Increase Traffic  →  Make Money Online  →  Recommended Things

Live Voting System Using Ajax And PHP.

Friday, Dec 15, 2017

Tags:- Ajax jQuery PHP MySQL

Live Voting System is nowadays a common way to get vote on particular thing. In this tutorial we will teach you how to make a simple and best live(without page refresh) voting system using Ajax and PHP.You may also like jQuery live preview.

See Demo
Voting System

To make a Live Voting System Using Ajax and PHP it takes only two steps:-

  1. Make a HTML form to post Vote
  2. Connect to the database and check and insert vote

Step 1.Make a HTML form to Post Vote

We make a HTML form with post method and save it with a name postvote.html

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
function postvote()
var all=document.getElementsByTagName("input");
for(var i=0;i<all.length;i++)
 if(all[i].type=="radio" && all[i].checked)
  var vote=all[i].value;
 type: 'post',
 url: 'insertvote.php',
 data: {
 success: function (response) {
  $( '#vote_status' ).html(response);

 $( '#vote_status' ).html("Please Select Language");

return false;


<form method="POST" onsubmit="return postvote();">
 <p>Which Is A Best Language?</p>
 PHP <input type="radio" name="yourvote" value="PHP">
 JSP <input type="radio" name="yourvote" value="JSP">
 ASP.Net <input type="radio" name="yourvote" value="ASP.Net">
 <input type="submit" name="submit_form" value="Vote">
<p id="vote_status"></p>


Now our Ajax request on postvote.html sends the voted value to insertvote.php file.

Step 2.Connect to the database and check and insert vote

We create a table named user_vote with two columns user_ip and vote.

CREATE TABLE user_vote(
user_ip VARCHAR(40),
vote TEXT);
// insertvote.php

$host = 'localhost';
$user = 'root';
$pass = '';
mysql_connect($host, $user, $pass);

if(isset($_POST[ 'vote_val' ]))

 $checkip=" SELECT user_ip FROM user_vote WHERE user_ip='$userip' ";

  echo "You Already Post Your Vote";
  $vote = $_POST[ 'vote_val' ];
  $insertdata=" INSERT INTO user_vote VALUES( '$userip','$vote' ) ";
  echo "You Vote Is Successfully Inserted";


We check if the user already post vote or not. If user IP address is already in our database it means that the user already post vote he will get "already vote" message and if the IP address is not in our database then user vote will be inserted.You may also like live character count using jQuery.

That's all, this is how to make a live voting system using Ajax and PHP.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Subscribe Our Newsletter And Get Tutorials And Offers Via Email

About Me

Hello, My Name is Mudit Jain. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of TalkersCode. I started this blog in february 2015 to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers.