Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos

Home » Ajax » Star Rating System Using PHP and Jquery with ajax

Star Rating System

Star Rating System Using PHP and Jquery with ajax

Posted on May 26, 2019December 17, 2022 By XpertPhp No Comments on Star Rating System Using PHP and Jquery with ajax

In this article, we are going to create add a star rating in php(Star Rating System Using PHP and Jquery with ajax). here We have considered a t-shirt as a product. When a customer buys a product that time adds the rating and comment to a product. so we will discuss about a 5 star rating system in php mysql.

Step1: Create the database and table.

Now, first, we create a database and create an item_rating table.

PHP
1
2
3
4
5
6
7
CREATE TABLE `item_rating` (
  `id` int(255) NOT NULL,
  `rate_number` varchar(255) NOT NULL,
  `title` varchar(255) NOT NULL,
  `comment` text NOT NULL,
  `created` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Step2: create the index.php file.

Second, we will the configuration of the database. after then we will create a rating system using the bootstrap. when we will select star on the click on the star. we will use ajax call to pass data after selecting a star, title, and comment. we will get rating data using the count_rate_data function.

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "rating";
 
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
 
$sql="SELECT SUM(rate_number) AS total_rate,count(id) as number_record FROM item_rating";
$result = mysqli_query($conn, $sql);
$data = mysqli_fetch_array($result);
 
function count_rate_data($rate_number)
{
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "rating";
 
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    $sql="SELECT count(id) as number_record FROM item_rating where rate_number='".$rate_number."' ";
    $result = mysqli_query($conn, $sql);
    $data = mysqli_fetch_array($result);
    if(!empty($data['number_record']))
    {
        return $data['number_record'];
    }
    else{
        return '0';
    }
 
}
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdn.jsdelivr.net/jquery.validation/1.15.1/jquery.validate.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
  <!-- Font Awesome Icon Library -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
* {  box-sizing: border-box;} body {  font-family: Arial;  margin: 0 auto;   max-width: 800px;  padding: 20px; }
.heading {  font-size: 25px;  margin-right: 25px; } .fa {  font-size: 25px; } .checked {  color: orange; }
.side {  float: left;  width: 15%;  margin-top:10px; } .middle {  margin-top:10px;  float: left;  width: 70%; }
.right {  text-align: right; } .row:after {  content: "";  display: table;  clear: both; }
.bar-container {  width: 100%;  background-color: #f1f1f1;  text-align: center;  color: white; }
.bar-5 {width: <?php echo (count_rate_data('5')*100)/$data['number_record'];  ?>%; height: 18px; background-color: #4CAF50;} .bar-4 {width: <?php echo (count_rate_data('4')*100)/$data['number_record'];  ?>%; height: 18px; background-color: #2196F3;} .bar-3 {width: <?php echo (count_rate_data('3')*100)/$data['number_record'];  ?>%; height: 18px; background-color: #00bcd4;}
.bar-2 {width: <?php echo (count_rate_data('2')*100)/$data['number_record'];  ?>%; height: 18px; background-color: #ff9800;} .bar-1 {width: <?php echo (count_rate_data('1')*100)/$data['number_record'];  ?>%; height: 18px; background-color: #f44336;}
label.error{ color:red; } .well{ margin-top: 25px; } .rating_bar{ margin-top: 20px; } .view_rating{ border-bottom: 1px solid #e3e3e3; padding: 20px; } .view_rating:last-child { border: none; }
@media (max-width: 400px) {  .side, .middle {    width: 100%;  }  .right {    display: none;  }}
</style>
<script>
$(document).ready(function(){
    $('.rating .fa-star').click(function(){
        if($(this).hasClass('checked')) {
            $(this).toggleClass('checked');
            $(this).prevAll('.fa-star').addClass('checked');
            $(this).nextAll('.fa-star').removeClass('checked');
        }
        else
        {
            $(this).toggleClass('checked');
            $(this).prevAll('.fa-star').addClass('checked');
        }
        $("#hdnRateNumber").val($('.checked').length);
    });
    $("#frmRating").validate({
        rules: {
            txtTitle: {
                required: true
            },
            txtComment: {
                required: true
            }
        },
        submitHandler: function (form) {
            //Your code for AJAX starts
            jQuery.ajax({
                url: 'saveData.php',
                type: "post",
                data: $(form).serialize(),
                success: function (data) {
                    if(data=="success")
                    {
                        window.location.reload();
                    }
                },
                error: function () {
                }
            });
        }
    });
});
</script>
</head>
<body>
 
<div class="row">
    <div class="col-lg-3"><img src="t-shirt.jpg" width="175"></div>
    <div class="col-lg-9">
        <p><b>Men Maroon Printed Round Neck T-shirt</b></p>
        <p>PRODUCT DETAILS</p>
        <p>Rust solid T-shirt, has a round neck, long sleeves</p>
        <p>Material & Care</p>
        <p>Cotton</p>
        <p>Machine-wash</p>
    </div>
</div>
<div class="row rating_bar">
    <div class="col-lg-12">
        <div class="row">
            <div class="col-lg-10">
                <p> <span class="btn btn-warning btn-xs"><?php echo $data['total_rate']/$data['number_record'];?></span> average based on <?php echo $data['number_record']; ?> reviews.</p>
            </div>
            <div class="col-lg-2">
                <button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#myModal">Rate Product</button>
            </div>
        </div>
        <hr style="border:3px solid #f1f1f1">
        <div class="row">
            <div class="col-lg-12">
                <div class="side"><div>5 star</div></div>
                <div class="middle">
                    <div class="bar-container">
                        <div class="bar-5"></div>
                    </div>
                </div>
                <div class="side right"><div><?php echo count_rate_data('5'); ?></div></div>
                <div class="side"><div>4 star</div></div>
                <div class="middle">
                    <div class="bar-container">
                        <div class="bar-4"></div>
                    </div>
                </div>
                <div class="side right"><div><?php echo count_rate_data('4'); ?></div></div>
                <div class="side"><div>3 star</div></div>
                <div class="middle">
                    <div class="bar-container">
                        <div class="bar-3"></div>
                    </div>
                </div>
                <div class="side right"> <div><?php echo count_rate_data('3'); ?></div></div>
                <div class="side"><div>2 star</div></div>
                <div class="middle">
                    <div class="bar-container">
                        <div class="bar-2"></div>
                    </div>
                </div>
                <div class="side right"><div><?php echo count_rate_data('2'); ?></div></div>
                <div class="side"><div>1 star</div></div>
                <div class="middle">
                    <div class="bar-container">
                        <div class="bar-1"></div>
                    </div>
                </div>
                <div class="side right"><div><?php echo count_rate_data('1'); ?></div></div>
            </div>
        </div>
    </div>
</div>
 
<div class="well">
<?php
$sql="SELECT * FROM item_rating ORDER by id DESC ";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)) {
?>
 
<div class="row view_rating">
    <div class="col-lg-3">
        <img src="user-dummy.png" width="75">
        <p><?php echo date('d M, Y',strtotime($row['created']));?></p>
    </div>
    <div class="col-lg-9">
        <p><span class="btn btn-warning btn-xs"><?php echo $row['rate_number'];?> <span class="fa fa-star" style="font-size: 12px;"></span></span> <?php echo $row['title'];?></p>
        <p><?php echo $row['comment'];?></p>
    </div>
</div>
<?php } ?>
</div>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
 
        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <div class="modal-body">
                <div class="rating">
                    <span class="heading">User Rating</span>
                    <span class="fa fa-star"></span>
                    <span class="fa fa-star"></span>
                    <span class="fa fa-star"></span>
                    <span class="fa fa-star"></span>
                    <span class="fa fa-star"></span>
                </div>
                <form name="frmRating" id="frmRating">
                    <div class="form-group">
                        <label for="txtTitle">Title:</label>
                        <input type="hidden" name="hdnRateNumber" id="hdnRateNumber">
                        <input type="text" class="form-control" id="txtTitle" placeholder="Enter Title" name="txtTitle">
                    </div>
                    <div class="form-group">
                        <label for="pwd">Comment:</label>
                        <textarea name="txtComment" id="txtComment" class="form-control" rows="10"></textarea>
                    </div>
                    <button type="submit" class="btn btn-default">Submit</button>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
 
    </div>
</div>
 
</body>
</html>

Step3: Create a saveData.php file.

In this step, we will store data into the database such as rate number, title, and comment.

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
 
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "rating";
 
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
 
 
 
if($_POST['txtTitle'] && $_POST['txtComment'])
{
 
    $rate = $_POST['hdnRateNumber'];
    $title = $_POST['txtTitle'];
    $comment = $_POST['txtComment'];
    $date = date('Y-m-d H:i:s');
    $sql = "INSERT INTO item_rating (rate_number,title,comment,created) VALUES ('".$rate."', '".$title."', '".$comment."','".$date."')";
 
    if (mysqli_query($conn, $sql) === TRUE) {
        echo "success";
    } else {
        echo "error";
    }
 
}
 
?>

Show Demo

See also  laravel eloquent WhereNotBetween query example
Ajax, Jquery, MySql, Php Tags:form validation, jQuery Ajax Form Handling, jquery tutorial, learn javascript and jquery, learn jquery, learn jquery online, php with mysql, rating system in php, udemy jquery

Post navigation

Previous Post: How to install node js on ubuntu 16.04 and 18.04
Next Post: AngularJS Form Validation Example

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com