Hey, I'm new to PHP And Ajax. I'm pretty good with SQL Though. I was wondering how I could an image, and when you click on it, run an ajax request to run a sql query
mysql_query("INSERT INTO `favorites` (`uid`, `gid`, `added`) VALUES ($member, $id, '$datetime');");
and update the image to a delete from favorites button.
using session for the user id (uid) and gid from index.php?id=13
to
http://davidknag.com/unfav.png
when clicking the unfavorite button delete that entry from sql.
I have never used ajax before and rarely used javascript.
So ajax with jQuery on image class click....
window.onload = function(){
$('.image').click(function(){
var image_id = $(this).attr('id');
$.ajax({
type: "POST",
url: "/ajaxpage.php",
data: {
image_id:image_id
}
success: function(data){
alert(data);
$('#'+image_id).remove().appendTo('#removed');
},
failure: function(){
alert('failed');
}
});
});
}
In your php file, you can take the necessary data (the id of the image or whatever) from the $_POST variable and execute your query. Echo some sort of string as a response, and then in the success handler of your ajax call append appropriately. Speak up if you run into difficulty, or have specific questions.
I'd go with JQuery
http://api.jquery.com/jQuery.post/
I was just about to type some code, but @Orbit beat me to it... +1 to Orbits answer.