I assume that you want a smaller “rating” system (as mentioned in other answers) and much more from the “add this to favorites” system?
Something like this should start you in the right direction. Others, feel free to call other best practices if you have any.
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],
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); ?>
maček
source share