Bladephp: Now with 8+ codeigniter, php, and laravel project 8+ php project From $37/Month From Rs. 2499/Month

How to Create Live Voting System using Ajax and PHP

php ajax mysql jquery
How to Create Live Voting System using Ajax and PHP

In this tutorial, we are going to create a simple voting system using PHP and jQuery AJAX. We will creating own Poll system (AJAX) for your projects with PHP. Polls, answers and results I going to keep in single SQL table. You may also like Live Search in php with Ajax and Mysql and How to Live Preview using jQuery

HTML and jQuery Code

        <script src=""></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)
                        type: 'post',
                        url: 'insertvote.php',
                        data: {
                            vote_val: vote
                        success: function (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">
            jQuery <input type="radio" name="yourvote" value="jQuery">
            ASP.Net <input type="radio" name="yourvote" value="ASP.Net">
            <input type="submit" name="submit_form" value="Vote">
        <p id="vote_status"></p>


PHP Code (insertvote.php)

define('DB_SERVER', "localhost");
define('DB_USER', "root");
define('DB_PASS', "");
define('DB_DATABASE', "whatsappdwld_db");
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);

if (isset($_POST['vote_val'])) {
    $userip = $_SERVER['REMOTE_ADDR'];
    $checkip = " SELECT user_ip FROM user_vote WHERE user_ip= '".$userip."' ";
    $query = mysqli_query($con, $checkip);
    if (mysqli_num_rows($query) > 0) 
        echo "You Already Post Your Vote";
        $vote = $_POST['vote_val'];
        $insertdata = " INSERT INTO user_vote (user_id, user_vote)VALUES( '$userip','$vote' ) ";
        echo "You Vote Is Successfully Inserted";


Html code work in comment box

Related Post