Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm assuming you want less of a "rating" system (as mentioned in the other answers) and more of a "add this to favorites" system?</p> <p>Something like this should get you started in the right direction. Others, feel free to chime in with other best-practices if you have them.</p> <h2>foo.html</h2> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script src="jquery.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="jquery.make_favorite.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function(){ $('.favorite').make_favorite(); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;a href="#article-15" class="favorite"&gt; &lt;img src="star.gif" alt="Make it a favorite!" /&gt; &lt;/a&gt; &lt;a href="#image-12" class="favorite"&gt; &lt;img src="star.gif" alt="Make it a favorite!" /&gt; &lt;/a&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <h2>jquery.make_favorite.js</h2> <pre><code>(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); </code></pre> <h2>favorite.php</h2> <pre><code>&lt;?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' =&gt; true, 'message' =&gt; 'Huzza!'); } else { $response = array('ok' =&gt; false, 'message' =&gt; mysql_error()); } // the magic? echo json_encode($response); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload