我假设您想要更少的“评级”系统(如其他答案中所述)而更多的“将其添加到收藏夹”系统?
这样的事情应该会让你朝着正确的方向开始。其他人,如果您有其他最佳实践,请随时提出建议。
foo.html
<html>
<head>
<script src="jquery.js" type="text/javascript"></script>
<script src="jquery.make_favorite.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.favorite').make_favorite();
});
</script>
</head>
<body>
<a href="#article-15" class="favorite">
<img src="star.gif" alt="Make it a favorite!" />
</a>
<a href="#image-12" class="favorite">
<img src="star.gif" alt="Make it a favorite!" />
</a>
</body>
</html>
jquery.make_favorite.js
(function($){
$.fn.make_favorite = function(){
var callback = function(response){
console.log(response);
};
return this.each(function(){
$(this).click(function(){
var params = {
item_type: $(this).attr('href').match(/\w+/)[0], // 'article'
item_id: $(this).attr('href').match(/\d+/)[0] // 15
};
$.post('/favorite.php', params, callback, 'json');
// stop event propagation
return false;
});
});
};
})(jQuery);
favorite.php
<?php
// make_favorite function
function make_favorite($item_type, $item_id){
return mysql_query(
sprintf("insert into favorites (user_id, item_type, item_id) values (%d, '%s', %d);", $_SESSION['user_id'], $item_type, $item_id)
);
}
// set header
header('Content-type: application/json');
// ensure to cleanse these inputs
$item_type = $_POST['item_type'];
$item_id = $_POST['item_id'];
if(make_favorite($item_type, $item_id)){
$response = array('ok' => true, 'message' => 'Huzza!');
}
else {
$response = array('ok' => false, 'message' => mysql_error());
}
// the magic?
echo json_encode($response);
?>