Join TalkersCode Now !

Get Latest Tutorials And Links On Web Development
And Get Existing Offers Via Email

Live Voting System Using Ajax And PHP.

Tuesday, Apr 4, 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.

Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email

Are You Starting A New Blog
Do You Want Best Web Hosting?

Try World's Best Web Hosting Provider BlueHost
At Just $3.95/mo

Hurry Up! Limited Time Offer

Yes, I Want To Check Out BlueHost

Check The World's Best SEO And All-In-One Marketing Toolkit
For Digital Marketing Professionals SEMRUSH