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.
CHECK OUT THIS TUTORIAL LIVE DEMO →
To make a Live Voting System Using Ajax and PHP it takes only two steps:-
- Make a HTML form to post Vote
- 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
<html> <head> <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; } } if(vote) { $.ajax({ type: 'post', url: 'insertvote.php', data: { vote_val:vote }, success: function (response) { $( '#vote_status' ).html(response); } }); } else { $( '#vote_status' ).html("Please Select Language"); } return false; } </script> </head> <body> <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"> </form> <p id="vote_status"></p> </body> </html>
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 <?php $host = 'localhost'; $user = 'root'; $pass = ''; mysql_connect($host, $user, $pass); mysql_select_db('demo'); if(isset($_POST[ 'vote_val' ])) { $userip=$_SERVER['REMOTE_ADDR']; $checkip=" SELECT user_ip FROM user_vote WHERE user_ip='$userip' "; $query=mysql_query($checkip); if(mysql_num_rows($query)>0) { echo "You Already Post Your Vote"; } else { $vote = $_POST[ 'vote_val' ]; $insertdata=" INSERT INTO user_vote VALUES( '$userip','$vote' ) "; mysql_query($insertdata); echo "You Vote Is Successfully Inserted"; } exit(); } ?>
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.
Latest Tutorials
- Create Animated Skill Bar Using jQuery, HTML And CSS
- Simple And Best Responsive Web Design Using CSS Part 2
- Show Button On Hover Using HTML And CSS
- Material Design Login Form Using jQuery And CSS
- Animated Progress Bar Using jQuery And CSS
- Dynamic Drop Down Menu Using jQuery, Ajax, PHP And MySQL
- Get Website Statistics Using PHP, jQuery And MySQL
- HTML5 Login And Signup Form With Animation Using jQuery
- Facebook Style Homepage Design Using HTML And CSS
- View Webpage In Fullscreen Using jQuery