[jQuery] Change user status with AJAX
I am sorry if this was posted twice!
-----------------------------------------------------------------------------------
Hi,
I have a list of users that I pull out of a MySQL DB. Some are
"favorite" users marked with a star.
Now I want to change the state "is favorite/is not favorite" quickly
with a click on the star image, and the image should turn directly to
its state yellow(is favorite) or grey(is not favorite).
This is how the link and image looks like:
<a href="{$user.id}" class="makeFavorite" id="makeFavorite-{$user.id}"
title="Save as favorite"><img src="images/{if $user.favorit ==
1}favorites.png{else}favorites2.png{/if}" width="16" height="16"
alt="" /></a>
The JS look like this:
// Make Favorite
$('.makeFavorite').click(function() {
$.ajax({
type: "POST",
url: "make_favorite.php",
data: "id=19",
success: function(msg) {
alert('Data saved: ' + msg);
}
});
return false;
});
The PHP:
<?php
…mysql connect …
$sql = "UPDATE table SET favorite='1' WHERE id=$_POST[id]";
mysql_query($sql);
echo 'My Msg: id is: ' . $_POST['id'];
?>
But unfortunately it doesn't work :(
The Msg says "My Msg: id is 19" but the state in the DB won't update.
Since this is the very first time I'm dealing with AJAX it is a quite
big thing for me :-)
What is wrong with the code above? And how can I change the image
directly without reloading the page?
Hope you get what I want to do and can help me. Thanks!