Jquery + PHP - AJAX onclick favorite/unfavorite

Discussion in 'jQuery' started by dlow123, Oct 24, 2008.

  1. #1
    Hello DPers,

    I am trying to build out a quick and dirty favorite feature for my site using Jquery and a little bit of ajax.

    Similar to the one used here on this arcade site.

    So far I have tried using a simple php GET request as well as a POST request, nothing seems to work.

    Here is the Javascript code I have thus far:

    
    --- JAVASCRIPT ---
    
    $(document).ready(function() {
    
    	favGame();
    
    	// Favorite Link //
    	function favGame() {
    			
    		$("a.arfavorite").click(function() {
    			$.get("favorites.php", {gid: gid, ar: ar}, function() {
    				alert("it all worked");
    			});
    			return false;
    		});
    	}
    
    });
    
    --- JAVASCRIPT ---
    
    Code (markup):
    Here is the PHP code I have thus far:
    
    --- PHP ---
    
    if (!isset($favorites[$game['id']])) {
    	echo "<a title='Add Favorite' class='arfavorite' href='profile/favorites.php' rel='nofollow'>Add To My Favorites</a>";
    } else {
    	echo "<a title='Remove Favorite' class='arfavorite' href='profile/favorites.php' rel='nofollow'>Remove From My Favorites</a>";
    }
    
    --- PHP ---
    
    Code (markup):
    :confused:
     
    dlow123, Oct 24, 2008 IP