To create this simple voting system, it is necessary to have made in a reasonable MySql database with an established connection.
Also, it is necessary to create a table in the database
<?php
$servername = "localhost";
$username = "yourusername";
$password = "yourpassword";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "create database demo";
if ($conn->query($sql) === TRUE) {
echo "Database demo created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>
<?php
$con=mysqli_connect("localhost","yourusername","yourpassword","demo");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "CREATE TABLE IF NOT EXISTS votes (
id INT AUTO_INCREMENT,
rating INT(11) NOT NULL,
PRIMARY KEY (id));";
if (mysqli_query($con,$sql)) {
echo "Table `votes` created successfully";
} else {
echo "Error creating table: " . mysqli_error($con);
}
?>
The data in the table can insert manually using phpmyadmin or using "insertData.php" code for insert data to MySql table. For example:
<?php
$conn=mysqli_connect("localhost","yourusername","yourpassword","demo");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "INSERT INTO votes(rating)
VALUES(0);";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
<?php
$servername = "localhost";
$username = "yourusername";
$password = "yourpassword";
$dbname = "demo";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if(isset($_POST["rating"])) {
$ql=mysqli_query($conn,"UPDATE votes SET rating=rating+1 WHERE id=1");
}
$sql = "SELECT id, rating FROM votes WHERE id=1";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo $row["rating"];
}
}
?>
Finally, it is necessary to create an index web page where data from the mjskl database will be displayed and from which new values will be sent to the MySql database.
By using the AJAX and jQUERY methods, we will avoid rereading the entire web page when sending and reading data from the server. Due to this, only certain parts of the website will be refreshed.
In order to do this, it is necessary to attach a link with jQUERY libraries.
<!DOCTYPE html>
<html lang='en'>
<head>
<title>Reload Div Content without reloading the web page using AJAX jQuery</title>
</head>
<body>
<p id='test'></p>
<form>
<input id='demo' type ='submit' name='rating' value ='vote' />
</form>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js'></script>
<script>
$(document).ready(function(){
$.get('vote.php', function(data){
$('#test').html(data);
});
$('#demo').click(function(e){
e.preventDefault();
$.ajax({
url:'vote.php',
type:'POST',
data:'rating',
dataType:'text',
cache: 'false',
success:function(data){
$('#test').html(data);
}
});
});
});
</script>
</body>
</html>