Simple Polling System using PHP/MySQLi with Source Code
This PHP Polling System written in PHP and MySQL. We will teach how to create this system for your schools, business, etc. This system can vote and counts the result for each participants or candidates in the given data by the admin. Each user has a account and can create a new account if they want to vote. The users can also manage their profiles if they want. And for the admin side the admin only can view the total poll of records in each candidates if who are the winner or who has a higher votes that the users chooses. See the example code below.
Sample Code
login.html
- This is for the html form for login script for the UI.
- <!DOCTYPE html>
- <html>
- <head>
- <link href="css/user_styles.css" rel="stylesheet" type="text/css" />
- <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
- </head>
- <body bgcolor="tan">
- <div id="page">
- <div id="header">
- </div>
- <div id="container">
- <form name="form1" method="post" action="checklogin.php" onsubmit="return loginValidate(this)">
- <div class="form-group">
- <input type="email" name="myusername" class="form-control" id="myusername">
- </div>
- <div class="form-group">
- <input type="password" name="mypassword" class="form-control" id="mypassword">
- </div>
- <div class="checkbox">
- </div>
- </form>
- <center>
- </center>
- </div>
- <div id="footer">
- </div>
- </div>
- </body>
- </html>
checklogin.php
- This file is for the checking of the users' login if the user is existing in the database.
- <!DOCTYPE html>
- <html>
- <head>
- <title>PHP Polling System</title>
- <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
- <link href="css/user_styles.css" rel="stylesheet" type="text/css" />
- </head>
- <body bgcolor="tan">
- <center><b><font color="#000" size="6">PHP Polling System</font></b></center><br><br>
- <body>
- <div id="page">
- <div id="header">
- <h1>Invalid Credentials Provided </h1>
- <p align="center"> </p>
- </div>
- <div id="container">
- <?php
- $host="localhost";
- $username="root";
- $password="";
- $db_name="poll";
- $tbl_name="tbMembers";
- $myusername=$_POST['myusername'];
- $mypassword=$_POST['mypassword'];
- $sql="SELECT * FROM $tbl_name WHERE email='$myusername' and password='$encrypted_mypassword'" or die(mysqli_error($conn));
- if($count==1){
- $_SESSION['member_id'] = $user['member_id'];
- }
- else {
- echo "Wrong Username or Password<br><br>Return to <a href=\"login.html\">login</a>";
- }
- ?>
- </div>
- <div id="footer">
- <div class="bottom_addr">Sourcecodester © @2016</div>
- </div>
- </div>
- </body>
- </html>
vote.php
- This script is for the counting of votes that every user clicks the radio button and submits.
- <?php
- <?php
- }
- ?>
- <?php
- ?>
- <?php
- {
- }
- else
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>PHP Polling System</title>
- <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
- <link href="css/user_styles.css" rel="stylesheet" type="text/css" />
- <script language="JavaScript" src="js/user.js"></script>
- <script language="JavaScript" src="js/jquery-1.2.6.min.js"></script>
- <script type="text/javascript">
- function getVote(int)
- {
- if (window.XMLHttpRequest)
- {
- xmlhttp=new XMLHttpRequest();
- }
- else
- {
- xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
- }
- xmlhttp.open("GET","save.php?vote="+int,true);
- xmlhttp.send();
- }
- function getPosition(String)
- {
- if (window.XMLHttpRequest)
- {
- xmlhttp=new XMLHttpRequest();
- }
- else
- {
- xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
- }
- xmlhttp.open("GET","vote.php?position="+String,true);
- xmlhttp.send();
- }
- </script>
- <script type="text/javascript">
- $(document).ready(function(){
- var $ = jQuery.noConflict();
- $(document).ready(function()
- {
- // setInterval(function(i){
- // $.ajax({
- // url: "admin/refresh.php",
- // cache: false,
- // success: function(html){
- // $(".refresh").html(html);
- // }
- // })
- // },1000)
- });
- $('.refresh').css({color:"green"});
- });
- </script>
- </head>
- <body bgcolor="tan">
- <center><b><font color="#000" size="6">PHP Polling System</font></b></center><br><br>
- <div id="page">
- <div id="header">
- <h1>CURRENT POLLS</h1><hr/>
- <ul class="nav navbar-nav"><li><a href="student.php"><h1>Home</h1></a></li><li><a href="vote.php"><h1>Current Polls</h1></a></li><li><a href="manage-profile.php"><h1>Manage My Profile</h1></a></li><li><a href="logout.php"><h1>Logout</h1></a></li></ul>
- </div>
- <div class="refresh"></div>
- <div id="container">
- <table width="420" align="center">
- <form name="fmNames" id="fmNames" method="post" action="vote.php" onsubmit="return positionValidate(this)">
- <tr>
- <td>Choose Position</td>
- <td><SELECT NAME="position" id="position" onclick="getPosition(this.value)">
- <OPTION VALUE="select">select
- <?php
- echo "<OPTION ".( isset($_POST['position'] ) && $row['position_name'] == $_POST['position'] ? 'selected' : '').">$row[position_name]";
- }
- ?>
- </SELECT></td>
- <td><input type="submit" name="Submit" value="See Candidates" /></td>
- </tr>
- </form>
- </table>
- <table width="270" align="center">
- <form>
- <tr>
- <th>Candidates:</th>
- </tr>
- <?php
- echo "<tr>";
- echo "<td>" . $row['candidate_name']."</td>";
- echo "<td><input type='radio' name='vote' value='$row[candidate_name]' onclick='getVote(this.value)' /></td>";
- echo "</tr>";
- }
- }
- else
- ?>
- </form>
- </table>
- </div>
- <div id="footer">
- <div class="bottom_addr">Sourcecodester © @2016</div>
- </div>
- </body>
- </html>
manage-profile.php
- And for the managing of each users account.
- <?php
- }
- $result = null;
- }
- if($row)
- {
- $stdId = $row['member_id'];
- $firstName = $row['first_name'];
- $lastName = $row['last_name'];
- $email = $row['email'];
- }
- ?>
- <?php
- $myEmail = $_POST['email'];
- $myPassword = $_POST['password'];
- $sql = mysqli_query($conn, "UPDATE tbMembers SET first_name='$myFirstName', last_name='$myLastName', email='$myEmail', password='$newpass' WHERE member_id = '$myId'" )
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>PHP Polling System</title>
- <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
- <link href="css/user_styles.css" rel="stylesheet" type="text/css" />
- <script language="JavaScript" src="js/user.js"></script>
- </head>
- <body bgcolor="tan">
- <center><b><font color="#000" size="6">Simple PHP Polling System</font></b></center><br><br>
- <div id="page">
- <div id="header">
- <h1>MANAGE MY PROFILE</h1><hr/>
- <ul class="nav navbar-nav"><li><a href="student.php"><h1>Home</h1></a></li><li><a href="vote.php"><h1>Current Polls</h1></a></li><li><a href="manage-profile.php"><h1>Manage My Profile</h1></a></li><li><a href="logout.php"><h1>Logout</h1></a></li></ul>
- </div>
- <div id="container">
- <td>
- <table width="380" align="center">
- <CAPTION><h3>MY PROFILE</h3></CAPTION>
- <tr>
- <td>Student Id:</td>
- <td><?php echo $stdId; ?></td>
- </tr>
- <tr>
- <td>First Name:</td>
- <td><?php echo $firstName; ?></td>
- </tr>
- <tr>
- <td>Last Name:</td>
- <td><?php echo $lastName; ?></td>
- </tr>
- <tr>
- <td>Email:</td>
- <td><?php echo $email; ?></td>
- </tr>
- <tr>
- <td>Password:</td>
- <td>Encrypted</td>
- </tr>
- </table>
- </td>
- <CAPTION><hr/><h3>UPDATE PROFILE</h3></CAPTION>
- <form action="manage-profile.php?id=<?php echo $_SESSION['member_id']; ?>" method="post" onsubmit="return updateProfile(this)">
- <div class="form-group">
- <label for="name">First Name:</label>
- <input type="text" class="form-control" name="firstname">
- </div>
- <div class="form-group">
- <label for="name">Last Name:</label>
- <input type="text" class="form-control" name="lastname">
- </div>
- <div class="form-group">
- <label>Email Address:</label>
- <input type="text" class="form-control" name="email">
- </div>
- <div class="form-group">
- <label>New Password:</label>
- <input type="password" class="form-control" name="password">
- </div>
- <div class="form-group">
- <label>Confirm New Password:</label>
- <input type="password" class="form-control" name="ConfirmPassword">
- </div>
- <button type="submit" name="update" class="btn btn-default">Update Profile</button>
- </form>
- <hr>
- </div>
- <div id="footer">
- <div class="bottom_addr">Sourcecodester © @2016</div>
- </div>
- </div>
- </body>
- </html>
And for more forms and file of this project to be added just download the file from the download button below.
Features
Admin- Manage Administrator
- Manage Positions
- Manage Candidates
- View Poll Result
- Login/Logout
- Login/Logout
- Vote
- Manage Account
How to Run
Requirements- Download and Install any local web server such as XAMPP/WAMP.
- Download the provided source code zip file. (download button is located below)
- Open your XAMPP/WAMP's Control Panel and start the "Apache" and "MySQL".
- Extract the downloaded source code file.
- If you are using XAMPP, copy the extracted source code folder and paste it into the XAMPP's "htdocs" directory. And If you are using WAMP, paste it into the "www" directory.
- Browse the PHPMyAdmin in a browser. i.e. http://localhost/phpmyadmin
- Create a new database naming "poll".
- Import the provided SQL file. The file is known as "poll.sql" located inside the extracted source code folder.
- Browse the Simple Polling System in a browser. i.e. http://localhost/pollsys/login.html for the students/voters and http://localhost/pollsys/admin/login.html for the admin side
Admin Access:
Email: [email protected]
Password: admin
DEMO
I Hope that you have/will learn something useful from this tutorial. Don't forget to Like and Share.
Enjoy Coding!Comments
Add new comment
- Add new comment
- 4663 views