article

Saturday, February 13, 2021

Live voting System using PHP Mysql and jquery Ajax bootstrap progressbar

Live voting System using PHP Mysql and jquery Ajax bootstrap progressbar

--
-- Table structure for table `tblprogramming`
--

CREATE TABLE `tblprogramming` (
  `id` int(11) NOT NULL,
  `title` varchar(250) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `tblprogramming`
--

INSERT INTO `tblprogramming` (`id`, `title`) VALUES
(1, 'Flask'),
(2, 'Laravel'),
(3, 'React.js'),
(4, 'Express'),
(5, 'Django');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `tblprogramming`
--
ALTER TABLE `tblprogramming`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `tblprogramming`
--
ALTER TABLE `tblprogramming`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;

--
-- Table structure for table `tbl_poll`
--

CREATE TABLE `tbl_poll` (
  `poll_id` int(11) NOT NULL,
  `web_framework` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `tbl_poll`
--

INSERT INTO `tbl_poll` (`poll_id`, `web_framework`) VALUES
(8, 'Flask'),
(9, 'Flask'),
(10, 'Flask'),
(11, 'Express'),
(12, 'React.js'),
(13, 'Laravel'),
(14, 'Flask'),
(15, 'Flask'),
(16, 'Laravel'),
(17, 'Django'),
(18, 'Django'),
(19, 'Flask');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `tbl_poll`
--
ALTER TABLE `tbl_poll`
  ADD PRIMARY KEY (`poll_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `tbl_poll`
--
ALTER TABLE `tbl_poll`
  MODIFY `poll_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=20;
index.php
//index.php
<html>  
<head>  
<title>Live voting System using PHP Mysql and jquery Ajax bootstrap progressbar</title>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>	
</head>  
<body>  
<div class="container">  
<?php
 include('dbcon.php');
 $query = $conn->query("SELECT * FROM tblprogramming order by id asc");
?>
            <br />  
            <br />
			<br />
			<h2 align="center">Live voting System using PHP Mysql and jquery Ajax bootstrap progressbar</h2><br />
			<div class="row">
				<div class="col-md-6">
					<form method="post" id="poll_form">
						<h3>Which is Best Web Development Frameworks</h3>
						<br />
						<?php  while ($row = $query ->fetch_object()) { ?>
						<div class="radio">
							<label><h4><input type="radio" name="poll_option" class="poll_option" value="<?php echo $row->title; ?>" /> <?php echo $row->title; ?></h4></label>
						</div>
						<?php } ?>
						<br />
						<input type="submit" name="poll_button" id="poll_button" class="btn btn-primary" />
					</form>
					<br />
				</div>
				<div class="col-md-6">
					<br />
					<br />
					<br />
					<h4>Live Poll Result</h4><br />
					<div id="poll_result"></div>
				</div>
			</div>
			<br />
			<br />
			<br />
		</div>
<script>  
$(document).ready(function(){
	fetch_poll_data();
	function fetch_poll_data()
	{
		$.ajax({
			url:"polldata.php",
			method:"POST",
			success:function(data)
			{
				$('#poll_result').html(data);
			}
		});
	}
	$('#poll_form').on('submit', function(event){
		event.preventDefault();
		var poll_option = '';
		$('.poll_option').each(function(){
			if($(this).prop("checked"))
			{
				poll_option = $(this).val();
			}
		});
		if(poll_option != '')
		{
			$('#poll_button').attr('disabled', 'disabled');
			var form_data = $(this).serialize();
			$.ajax({
				url:"insert.php",
				method:"POST",
				data:form_data,
				success:function()
				{
					$('#poll_form')[0].reset();
					$('#poll_button').attr('disabled', false);
					fetch_poll_data();
					alert("Poll Submitted Successfully");
				}
			});
		}
		else
		{
			alert("Please Select Option");
		}
	});
});  
</script>
</body>  
</html>  
polldata.php
//polldata.php
<?php
include('dbcon.php');
$resulttotal = $conn->query("SELECT * FROM tbl_poll"); 
$total_poll_row = mysqli_num_rows($resulttotal);

$query = $conn->query("SELECT * FROM tblprogramming order by id asc");
$output = '';
while ($row = $query ->fetch_object()) {
		$title=$row->title; 
		$result = $conn->query("SELECT * FROM tbl_poll WHERE web_framework = '".$title."'"); 
		$row = $result->fetch_row();
		$total_row = mysqli_num_rows($result);
		$percentage_vote = round(($total_row/$total_poll_row)*100);
		$progress_bar_class = '';
		if($percentage_vote >= 40)
		{
			$progress_bar_class = 'progress-bar-success';
		}
		else if($percentage_vote >= 25 && $percentage_vote < 40)
		{
			$progress_bar_class = 'progress-bar-info';
		}
		else if($percentage_vote >= 10 && $percentage_vote < 25)
		{
			$progress_bar_class = 'progress-bar-warning';
		}
		else
		{
			$progress_bar_class = 'progress-bar-danger';
		}
		$output .= '
		<div class="row">
			<div class="col-md-2" align="right">
				<label>'.$title.'</label>
			</div>
			<div class="col-md-10">
				<div class="progress">
					<div class="progress-bar '.$progress_bar_class.'" role="progressbar" aria-valuenow="'.$percentage_vote.'" aria-valuemin="0" aria-valuemax="100" style="width:'.$percentage_vote.'%">
						'.$percentage_vote.' % programmer like <b>'.$title.'</b> PHP Framework
					</div>
				</div>
			</div>
		</div>
		
		';
}
echo $output;
?>
insert.php
//insert.php
<?php
include('dbcon.php');
if(isset($_POST["poll_option"]))
{
	$poll_option = $_POST["poll_option"];
    $sql = "INSERT INTO tbl_poll(web_framework) VALUES ('$poll_option')"; 
    $conn->query($sql);
}
?>
dbcon.php
//dbcon.php
<?php
$conn = new mysqli('localhost','root','','testingdb');
if ($conn->connect_error) {
    die('Error : ('. $conn->connect_errno .') '. $conn->connect_error);
}
?>

Related Post