PHP
OOPs
JavaScript
JQuery
Ajax
Codeigniter
Laravel
MySql
Node.js
Tutorials
C
C++
Json
Codeigniter 4
htaccess
wordpress
react.js
Blogs
Fundamental
Python
Search Your Query
×
--- Select Category ---
PHP
OOPs
JavaScript
JQuery
Ajax
Codeigniter
Laravel
MySql
Node.js
Tutorials
Blogs
Fundamental
Python
Most Popular Tutorials
What is Ajax
How Ajax Work in Php
ajax() and load() function
AJAX get() and post() Method
$.ajax() function
Get data using Ajax call
Load div content on button click
Insert Data in mysql Database
Delete Record
Fetch Data from database
User Registration form
ContactUs form
Change Password
ajax search
insert data usign Serialize
Insert Data without serialize function
User Check Availability
Star rating
Pin check availability
Dynamically Add / Remove input fields in PHP with Jquery Ajax
clear form field after submitting form
Loader show / hide
Javascript and Ajax function
Ajax Programs
Success and Failure message
getJSON Function in ajax
Tutorials Menus
What is Ajax
How Ajax Work in Php
ajax() and load() function
AJAX get() and post() Method
$.ajax() function
Get data using Ajax call
Load div content on button click
Insert Data in mysql Database
Delete Record
Fetch Data from database
User Registration form
ContactUs form
Change Password
ajax search
insert data usign Serialize
Insert Data without serialize function
User Check Availability
Star rating
Pin check availability
Dynamically Add / Remove input fields in PHP with Jquery Ajax
clear form field after submitting form
Loader show / hide
Javascript and Ajax function
Ajax Programs
Success and Failure message
getJSON Function in ajax
Share On Facebook
Star rating in php using ajax
Star Rating using php with ajax
File name : index.php
<html>
<head>
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css" rel="stylesheet">
<link href="css/star-rating.min.css" media="all" rel="stylesheet" type="text/css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="js/star-rating.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$('.rating').on('rating.change', function(event, value, caption) {
productId = $(this).attr('productId');
$.ajax({
url: "rating.php",
dataType: "json",
data: {vote:value, productId:productId, type:'save'},
success: function( data ) {
alert("rating saved");
},
error: function(e) {
// Handle error here
console.log(e);
},
timeout: 30000
});
});
});
</script>
</head>
<body>
<table border=1>
<tr><td >
<img src="img/p1.jpeg">
<h3>Product-1</h3>
<input value="0" type="number" class="rating" min=0 max=5 step=0.1 data-size="md" data-stars="5" productId=1>
</td>
<td>
<img src="img/p2.jpeg">
<h3>Product-2</h3>
<input value="0" type="number" class="rating" min=0 max=5 step=0.1 data-size="md" data-stars="5" productId=2>
</td>
</tr></table>
</body>
</html>
File name : rating.php
<?php
function connect() {
$hostname = "localhost";
$username = "root";
$password = "root";
$dbname = "test";
$con = mysqli_connect($hostname, $username, $password, $dbname);
return $con;
}
function getRatingByProductId($con, $productId) {
$query = "SELECT SUM(vote) as vote, COUNT(vote) as count from rating WHERE product_id = $productId";
$result = mysqli_query($con, $query);
$resultSet = mysqli_fetch_assoc($result);
if($resultSet['count']>0) {
$data['avg'] = $resultSet['vote']/$resultSet['count'];
$data['totalvote'] = $resultSet['count'];
} else {
$data['avg'] = 0;
$data['totalvote'] = $resultSet['count'];
}
return $data;
}
if(isset($_REQUEST['type'])) {
if($_REQUEST['type'] == 'save') {
$vote = $_REQUEST['vote'];
$productId = $_REQUEST['productId'];
$query = "INSERT INTO rating (product_id, vote) VALUES ('$productId', '$vote')";
// get connection
$con = connect();
$result = mysqli_query($con, $query);
echo 1; exit;
}
}
?>
Output :-
CREATE TABLE IF NOT EXISTS `rating` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` int(11) NOT NULL,
`vote` float NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Share On Facebook
Previous
Next
Trending Tutorials
PHP
100+ Tutorials
PHP OOPs
80+ Tutorial
Codeigniter
100+ Tutorial
Laravel
100+ Tutorials
NODE.JS
100+ Tutorials
Javascript
100+ Tutorials
Download Live Projects
Review & Rating
0.0
/ 5
0
Review
5
(
0
)
4
(
0
)
3
(
0
)
2
(
0
)
1
(
0
)
Write Review Here
Review
Submit Review
×
Submit
Ittutorial
Ittutorial
Most Popular Tutorials
Get data using Ajax call
$.ajax() function
User Check Availability
Dynamically Add / Remove input fields in PHP with Jquery Ajax
Pin check availability
Ajax Programs
ContactUs form
User Registration form
Insert Data in mysql Database
Load div content on button click
Star rating
What is Ajax
insert data usign Serialize
Fetch Data from database
clear form field after submitting form
ajax search
Javascript and Ajax function
Change Password
Delete Record
getJSON Function in ajax
Important Link
Core php Online Advance Tutorails and video Tutorials.
How to manage Session in php Tutorials.
More .......
upcomming codeigniter and angular js tutorials.
Click here to see More ...
Hello Friend Please Follow & Share Ittutorial.
×
Ittutorial
Please Follow and Share ittutorial.
Ittutorial