5 Star Rating plugin in PHP Script with Ajax

By | 03/12/2013

5 Star rating Plugin in php is too essential for a commercial site. Here is Our Rating plugin that works good with the Database. Rating  plugin works in PHP with Ajax. And it is too flexible for the customization. Here the rating php script

5-star

 

Demo Url for the Rating in PHP script    http://demo.getln.com/Simple%205%20Star%20Rating/

5 Star Rating plugin in PHP Script with Ajax

index.php

Select Code
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
<?php 
ob_start();
include("db.php");
?>
xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simple 5 Star Rating</title>
<script src="rating.js"></script>
<script type="text/javascript">
function sendRate(sel){
alert("Your rating was: "+sel.title);
}
function showCustomer(str)
{
var xmlhttp;
if (str.title=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
s = str.name.replace("_", '');
xmlhttp.open("GET","rate-ajax.php?rate="+str.title+"&id="+s,true);
xmlhttp.send();
}
</script>

<link href="ratings.css" rel="stylesheet" />
<h1>Simple 5 Star Rating-Tobby News</h1>
<?php
//enter the post_id or article or product which has to be rated
$post_id='1';
$id=$post_id;
$query=mysql_query("select * from ratings where art_id='$id'");
$query1=mysql_fetch_array($query);
$total_votes=$query1['total_votes'];
$total_points=$query1['total_points'];
if(mysql_num_rows($query))
{
$ratings=$total_points/$total_votes;
}
else
{
$ratings=0;
}
$rates=round($ratings);
?>
<div class="container">
<div id="rateMe">
<?php
for($i=1;$i<=5;$i++)
{
switch($i)
{
case 1:
$title="Very Poor";
$star=1;
break;
case 2:
$title="Poor";
$star=2;
break;
case 3:
$title="Good";
$star=3;
break;
case 4:
$title="Very Good";
$star=4;
break;
case 5:
$title="Excellent";
$star=5;
break;
}
$starred="";
if($star<=$rates)
{
$starred="class='on'";
}
echo "<a onClick='rateIt(this)' id='_".$i."' title='".$title."' onMouseOver='rating(this)' onMouseOut='off(this)'".$starred." name=".$post_id."></a>";
}
if($rates!=0)
{
echo "
<div style='margin-top:5px; margin-left: 135px;'>Rating ".round($ratings,2)."/5</div>
";
}
else
{
echo "<div style='margin-top: 5px;margin-left: 135px;'>Not Rated Yet</div>";
}
?>
<span id="rateStatus"></span>
<span id="ratingSaved"></span>
</div>
<div id="txtHint">
Not Yet Rated
</div>
</div>

<hr style="border:0D8CCF 1px solid;border-top: 0;>
</body>
</html>

Ajax file for the Rating in php

rate-ajax.php

Select Code
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
<?php
ob_start();
include('db.php');
if((isset($_GET['id']))&&(isset($_GET['rate'])))
{
$id=$_GET['id'];
$string=$_GET['rate'];
switch($string)
{
case "Very Poor":
$vote=1;
break;
case "Poor":
$vote=2;
break;
case "Good":
$vote=3;
break;
case "Very Good":
$vote=4;
break;
case "Excellent":
$vote=5;
break;
}
//chack the article is voted already
$check_query=mysql_query("select * from ratings where art_id='$id'");
$voted=mysql_num_rows($check_query);
if($voted==0)
{
$vote_query=mysql_query("insert into ratings values('','$id','1','$vote')");
}
else
{
$upadte_vote=mysql_query("select * from ratings where art_id='$id'");
$upadte_vote1=mysql_fetch_array($upadte_vote);
$total_votes=$upadte_vote1['total_votes']+1;
$total_points=$upadte_vote1['total_points']+$vote;
$rate_id=$upadte_vote1['id'];
$vote_query=mysql_query("update ratings set total_votes='$total_votes', total_points='$total_points' where id='$rate_id'");
}
}
if($vote_query)
{
echo "Voting Succesfull";
}
?>

Include your database connection string in the db.php for rating php plugin.

And include our Rating Javascript by downloading it rating.js

Here the full plugin package of PHP Rating Code Download it!

Post By Praveen (38 Posts)

Website: →

Connect

About Praveen

Praveen S has written 38 post in this blog.

6 thoughts on “5 Star Rating plugin in PHP Script with Ajax

    1. Praveen Post author

      @ramesh please refer the full javascript in index.php and rate-ajax.php. You can change the ajax file instead of rate-ajax.php from index.php at line 39.
      rate-ajax.php will process the rating in php

      Reply
  1. Brenda

    Hi am I suppressed to set anything in the rating.js file? becasue after I rated it still says not rated, is there something I am supposed to do there?

    Reply
    1. Praveen Post author

      @brenda This Plugin Works fine. Please wait until the rating process is done. After completing the rating only the text will change to “Voting Successful”. You can change the “Not Yet Rated” to any text that you want.

      Reply
  2. Brenda

    I mean it shows this Rating 4/5 but still says not yet rated underneath – after I rated.
    Is there a number to reach? And if so is it just OK to not have that show or reach a limit?

    Reply
  3. Brenda

    Okay I think I see where I am confused, it might be better if I put “Rate this” or ” similar instead of “Not yet rated” it would make more sense, otherwise it looks like it never was by anyone.

    Reply

Leave a Reply