Hi,
I have a question about ajax, php si mysql..
In mysql i have a table playlists and here is the code for php:
<?php
$playlist_result = mysql_query("SELECT *, COUNT(playlist_id) AS NumOccurrences FROM playlists WHERE created_by=$user_id GROUP BY playlist_id HAVING ( COUNT(playlist_id) > 0 ) ORDER BY date DESC");
$playlist_result_count=mysql_num_rows($playlist_result);
$i=0;
if($playlist_result_count > 0)
{
while($row_playlist=mysql_fetch_assoc($playlist_result)) {
$playlist_name = $row_playlist['playlist_name'];
$playlist_id = $row_playlist['playlist_id'];
echo '<table width=100%><tr class=comment><td style=padding:2px; class=comment_box align=left><ul class=manager><li>';
echo '<a href='#' class='add_to_playlist'><span><b>'.$playlist_name.' - '.$playlist_id.'</b></span></a>';
echo '</li></ul></td></tr></table>';
$i++;
}
} else {
echo '<b>You don`t have any playlist yet</b>';
}
?>
Php shows all the playlists i have in my table, the name and id of the playlist. Then, the ajax part, when I click on the name of a playlist i want to insert the clicked playlist id to mysql.:
<script type='text/javascript'>
$(function(){
$("a.add_to_playlist").click(function(){
//get the id
var user_id = <?php echo $user_id; ?> ;
var id = <?php echo $itemid; ?>;
var playlist_id = <?php echo $playlist_id; ?>;
var parent = $(this);
// show the spinner
$(this).html("<img src='spinner.gif'/>");
//fadeout the vote-count
parent.fadeOut("fast");
//the main ajax request
$.ajax({
type: "POST",
data: "action=add_to_playlist&id=" + id + "&user_id=" + user_id + "&playlist_id=" + playlist_id,
url: "add_to_playlist.php",
success: function(msg)
{
parent.html(msg);
//fadein the vote count
parent.fadeIn("fast");
}
});
});
});
</script>
It`s working fine but in mysql is inserted the id of the last created playlist, not the one that I clicked.
What am I doing wrong?
Thanks for your help.
Bogdan