View Full Version : PHP/Ajax Rater Possible?

07-16-2007, 09:20 PM
I'm looking for a ready-made script that would allow me to host the rating device on one site and use it on another via PHP + Ajax. I'm thinking there must be something like this out there already. Basically something where I can host a script that would store the ids/ratings into a database and then have other websites that want to use the script simply use a javascript that would make calls to the php script residing on the server.

I can whip up something but it'll take a much longer time, ..anyone know if such a script exists or have any advice on how to begin?

07-16-2007, 09:32 PM

07-17-2007, 01:26 AM
I couldn't find anything even remotely usable. I created the Ajax interface, all I need is the backend, and I'm really really inexperienced with MySQL.

Are there any php/sql whizzes that could write a very simple script that would do this: When given a rating (between 1-5) would add it to a table called "votes" given the id.

Basically something like this,


In a table called votes, it looks for id=1 (if it doesn't exist, it inserts a new record with that ID) and the insert the rating=5, or add the rating to whatevers in there already. Maybe this is too much to do, I have no clue..I've written the entire frontend with ajax and over time I'll release it open-source..I just hope that someone can code something simple to just store and update the db.

I've looked around and all I can find are bloated rating scripts and tutorials that have too much garbage.

08-09-2007, 11:34 AM
I did it myself, it turned out pretty messy....worked well in firefox, but would not work at all in Internet Explorer - when the user clicked on the rating, it would pop open a new window in IE (without voting).

08-09-2007, 11:36 AM
hey I just built one of these the other day...


Check this link out. On the main page you will see (rate here) once you click that you will get 5 rating icons, click one and it cast the rating. If you were to double click it after you've voted then you can update your vote. Also feel free to help me in that thread as well :D.

08-09-2007, 11:48 AM
hey it works, not bad. You releasing the source though? lol I mean mines in really bad shape because it won't work in IE, BTW double clicking on it doesn't work for me, but hey at least it lets me vote.

08-11-2007, 09:59 AM
I just sent you a PM with the link. I'll transition the code to be more abstract so I can package it and allow it to be used by others. As for right now, any help you need with installing it...just ask. :thumbsup:

P.S.- What browser are you using? It has worked on Opera 9, IE 6, IE 7, Firefox 1.5, Firefox 2.0,Avant and Safari (on Macintosh). Are you using Netscape by chance? I must install that on my system when I get a chance.

08-11-2007, 08:41 PM
This would work if the articles/blahh where stored in a mysql database.

arcticle.php?id=x --

//mysql connect

//select article from DB
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM articles WHERE id='$id' LIMIT 1")or die(mysql_error());
$row = mysql_fetch_array($result);
echo $row['article'];
echo "<center>";
//display form
echo "<b>Rate This Article</b><br>";
echo "<form method=post action=vote.php>";
echo "Rating <select name=rating><option style='background-color: red; color: white' value='1'>Bad - 1<option style='background-color: orange; color: white' value='2'>Blah - 2<option style='background-color: green; color: black' value='3'>Blah - 3<option style='background-color: lightgreen; color: black' value='4'>Blah - 4<option style='background-color: limegreen; color: black'value='5'>Blah - 5</select><br><input type='hidden' value='".$_GET['id']."' name='article'><input type=submit value=Rate></form>
vote.php --

//mysql connect

//fetch POST'd vars
$rating = $_POST['rating'];
$article = $_POST['article'];

//Local IP address

//See if IP address has voted -- If result proves positive, die, else, continue
$result = mysql_query("SELECT * FROM votes WHERE article='$article' AND ip='$ip' LIMIT 1")or die(mysql_error());
$row = mysql_fetch_array($result);
echo "<center><b>You've already voted!</b></center>";
mysql_query("INSERT INTO votes (article, ip, rating)VALUES('$article', '$ip', '$rating') ")or die(mysql_error());
header("Location: article.php?id=$article");

article {
article - Longtext;
id - AUTO_INCREMENT int(255);

rating - INDEX int(1);
ip - varchar(50);
article - int(255);

There's an example. I can recode it in a better format (ie. so it's all on one page) and stuff like that.

08-12-2007, 03:38 AM
this is my example, sorry i had it ready a couple of days ago but totally forgot to post it up for you. It has ajax as well so it does not have to reload the page.


<title>Rate me</title>
<script type="text/javascript" src="ajax.js"></script>

<body onload="Rate('0')">
<input type="button" value="1" onclick="Rate('1')" /><input type="button" value="2" onclick="Rate('2')" /><input type="button" value="3" onclick="Rate('3')" /><input type="button" value="4" onclick="Rate('4')" /><input type="button" value="5" onclick="Rate('5')" />
<br />Your Rating:
<div id="rating"></div>
Total amount of ratings:
<div id="amount"></div>


function createRequestObject() {
var xmlHttp;
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
catch (e)
// Internet Explorer
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
catch (e)
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
return xmlHttp;

var http = createRequestObject();

function Rate(action) {
http.open('get', 'rate.php?rating='+action, true);
http.onreadystatechange = handleResponse;

function handleResponse() {
if(http.readyState == 4){
var response = http.responseText;
var elements = new Array();
elements = response.split("|");
document.getElementById('rating').innerHTML = elements[1];
document.getElementById('amount').innerHTML = elements[0];

and rate.php

$rates = 0;
$rating = 0;
$connect = mysql_connect("my_server", "my_user", "my_pass");
mysql_select_db("rate", $connect);

if($_GET["rating"] != 0)
$rating = $_GET["rating"];

$sql = "INSERT INTO rating (rate) VALUES ('$rating')";

$sql = "SELECT * FROM rating";
$result = mysql_query($sql);

while($rate = mysql_fetch_array($result)) {
$rates = $rates + 1;
echo "$rates|$rating";

of course this is just an example, if i wanted it to be really fancy I would have images, e.g. stars, instead of buttons and do a nice mouseover effect to show the user how many stars they are rating it. also I would add extra rows to the table rate, like id and user so you can link the rates to the right object and only allow a user to rate on a particular item once

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum