views:

242

answers:

2

Hi everyone.

I am not familiar with js and jQuery, but need to create function to add/remove blog post to "My favorites" page and update counter of saved posts. Is any ready solution - plugin or snippet - to it?

There is my html snippet.

<h1><a href="http://www.example.com/add-post-to-my-favorites-page.htm" id="post_0064"><span class="bookmark" title="My Favorites — Add/Remove">Favorites </span>Heading</a></h1>
<p>Body copy.</p>
[...]
<ul class="ul_favs">
[...]
<li id="bookmarks" <a href="http://www.example.com/account/favs.htm"&gt;My Favorites</a><sup><!-- Counter -->46</sup></li>
</ul>

I think that it can be done with $.ajax, but have not idea how. Maybe something like that?

Thanks in advice.

+1  A: 

jQuery won't actually delete anything. If you want to really remove items, you'll have to do so at the source of the list. If your list is made up of static-HTML, you'll need a language like PHP that can access the raw-file and make changes. If your list is stored in a database, you'll need a server-side language like PHP or C# to make those changes.

jQuery can post data to server-side scripts that have the ability to remove/add/edit entries in a database. You might have a PHP script like the following:

if ($_POST) {
  $favid = $_POST["favid"];
  remove_favorite($favid);
}

jQuery could pass a favid to this script:

$.post("removefav.php", {favid:121});

This would post a variable to the server-side script, which would then take the value of that posted-variable and delete its corresponding record in the database.

This is a very crude example, but should suffice for getting you a bit more understanding of jQuery's relationship to server-side languages and databases.

Jonathan Sampson
Jonathan beat me to the answer. The best way to think about this is as a progressive enhancement (http://en.wikipedia.org/wiki/Progressive_enhancement) problem. Start with the HTML form that submits to the server (which returns your current favorites list). Once that is working, the javascript to manage the server calls can be done in three or four lines of jQuery.
Jason Francis
Thank you, guys
Vladimir
Johnathan, can you check: am I Moving in the right direction?function favorites() { $('.bookmark').click(function(event) { var request = $(this).parent.attr('id'); if ($(this).hasClass("ed")) { //Remove bookmark $.post("/account/favorites/remove.htm", { favid:request }, function() { //Decrease Counter $(this).toggleClass("ed"); }); } else { //Add bookmark $.post("/account/favorites/add.htm", { favid:request }, function() { //Increase Counter $(this).toggleClass("ed"); }); } });}
Vladimir
sorry for non-formated code. i'll repeate it in next answer
Vladimir
Jason, sorry, I don't use advice with progressive enhancement on this stage. It will be done later. Thanks
Vladimir
A: 
function favorites() {
    $(".bookmark").click(function(event) {
     var request = $(this).parent.attr("id");
     var counter = parseInt($("#bookmarks sup").text());
     if ($(this).hasClass("ed")) {
      //Remove bookmark
      $.post("/account/favorites/remove.htm", { favid:request },
        function() {
         $("#bookmarks sup").text(--counter); // Decrease counter
         $(this).toggleClass("ed"); //Toggle class of clicked element
        });
     } else {
      //Add bookmark
      $.post("/account/favorites/add.htm", { favid:request },
        function() {
         $("#bookmarks sup").text(++counter); // Increase counter
         $(this).toggleClass("ed"); //Toggle class of clicked element
        });
     }
    });
}
Vladimir