Chnage MySQL DB with AJAX.
Hello jQuery Pros!
I want to update a mysql db with jQuery and AJAX.
There is a star next to a username indicating that he's a favorite user and a grey star says he's not. I want to change the status (is favorite/is not favorite) quickly without reloading the page. But I have not done this before and I am quite new to JS and AJAX.
Here is what I have:
JS:
-
// Make Favorite
$('.makeFavorite').click(function() {
$.ajax({
type: "POST",
url: "make_favorite.php",
data: "id=19",
success: function(msg) {
alert('Data saved: ' + msg);
}
});
return false;
});
HTML Link:
-
<a href="{$user.id}" class="makeFavorite" id="makeFavorite-{$user.id}" title="Mark as favorite"> <img src="images/{if $user.favorit == 1}favorites.png{else}favorites2.png{/if}" width="16" height="16" alt="">
make_favorite.php
-
mysql connect…
$sql = "UPDATE table SET favorite='1' WHERE id=$_POST[id]";
mysql_query($sql);
echo 'id is: ' . $_POST['id'];
Although I get the Msg (Data saved…) the mysql db dosn't change. There must be something wrong, but I don't know what it is.
And how can I change the image after the update to display the new correct status?
Thanks for your help!