CREATE TABLE `articles` ( `ID` int(11) NOT NULL auto_increment, `a_title` varchar(255) NOT NULL, `a_subtitle` tinytext NOT NULL, `a_content` text NOT NULL, PRIMARY KEY (`ID`) ) ENGINE=MyISAM; INSERT INTO `articles` VALUES (1, 'This is the article title', 'This is the article subtitle', 'This is the article content This is the article content This is the article content This is the article content This is the article content '); INSERT INTO `articles` VALUES (2, 'This is the article title', 'This is the article subtitle', 'This is the article content This is the article content This is the article content This is the article content This is the article content '); CREATE TABLE `articles_ratings` ( `ID` int(11) NOT NULL auto_increment, `article_id` int(11) NOT NULL, `rating_value` tinyint(2) NOT NULL, `rater_ip` varchar(20) NOT NULL, PRIMARY KEY (`ID`) ) ENGINE=MyISAM;
<?php
require_once("db.php");
require_once("xajax/xajax.inc.php");
require("functions.php");
$xajax = new xajax();
$xajax->registerFunction("insert_rating");
$xajax->processRequests();
$query = mysql_query("SELECT * FROM articles WHERE ID ='".mysql_real_escape_string($_GET['ID'])."'");
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'."\n";
echo '<html xmlns="http://www.w3.org/1999/xhtml">'."\n";
echo ' <head>'."\n";
echo ' <title>Testing the PHP/Ajax rating script</title>'."\n";
echo ' '.$xajax->printJavascript();
echo ' </head>'."\n";
echo ' <body>';
if(mysql_num_rows($query)==1)
{
$row=mysql_fetch_assoc($query);
echo ' <h1>'.$row['a_title'];
echo '</h1>'."\n";
echo $row['a_content'];
echo ' <hr />'."\n";
echo ' <div id="response">'."\n";
echo ' <table>'."\n";
echo ' <tr>'."\n";
echo ' <td align="center" style="width:50px">1</td>'."\n";
echo ' <td align="center" style="width:50px">2</td>'."\n";
echo ' <td align="center" style="width:50px">3</td>'."\n";
echo ' <td align="center" style="width:50px">4</td>'."\n";
echo ' <td align="center" style="width:50px">5</td>'."\n";
echo ' </tr>'."\n";
echo ' <tr>'."\n";
echo ' <td align="center" style="width:50px"><input type="radio" name="rating" id="radio_1" onclick="xajax_insert_rating(1, '.$_GET['ID'].');" /></td>'."\n";
echo ' <td align="center" style="width:50px"><input type="radio" name="rating" id="radio_2" onclick="xajax_insert_rating(2, '.$_GET['ID'].');" /></td>'."\n";
echo ' <td align="center" style="width:50px"><input type="radio" name="rating" id="radio_3" onclick="xajax_insert_rating(3, '.$_GET['ID'].');" /></td>'."\n";
echo ' <td align="center" style="width:50px"><input type="radio" name="rating" id="radio_4" onclick="xajax_insert_rating(4, '.$_GET['ID'].');" /></td>'."\n";
echo ' <td align="center" style="width:50px"><input type="radio" name="rating" id="radio_5" onclick="xajax_insert_rating(5, '.$_GET['ID'].');" /></td>'."\n";
echo ' </tr>'."\n";
echo ' </table>'."\n";
echo ' </div>'."\n";
}
else {
echo 'There are no articles to display';
}
echo ' </body>';
echo '</html>';
?>
function insert_rating($rating, $article_id)
{
if(mysql_num_rows(mysql_query("SELECT ID FROM articles_ratings
WHERE rater_ip='".mysql_real_escape_string(getIP())."'
AND ID='".mysql_real_escape_string($article_id)."'"))==0)
{
if(mysql_query("INSERT INTO articles_ratings
(`article_id`, `rating_value`, `rater_ip`)
VALUES ('".mysql_real_escape_string($article_id)."',
'".mysql_real_escape_string($rating)."',
'".mysql_real_escape_string(getIP())."')"))
{
$response = 'Thank you for voting this article!';
}
else {
$response = 'Ups. A problem. I was unable to save your rating!';
}
}
else {
$response = 'Sorry but you can only rate once';
}
$objResponse = new xajaxResponse();
$objResponse->addAssign("response","innerHTML", $response);
return $objResponse;
}
<input type="radio" name="rating" id="radio_4" onclick="xajax_insert_rating(4, '.$_GET['ID'].');" />
No. |
Attachments |
Downloads |
|
|
1 |
Ajax/PHP rating script Archive containing the Ajax/PHP rating script files Uploaded on: 2007-09-21 | 08:02 am |
951 |
|
2008-05-15 | 07:21 am
2008-03-26 | 08:27 pm
2008-01-18 | 04:07 pm