In my .php-File, I do the following:
<a href="php/make_favorite.php" title="sn" alt=" Favorit " target="_self"><img src="image.jpg" width="40" height="30" alt="Zgen"></img></a></br>
So if somebody clicks on this image, I call a php and the actual recipe will be marked as favourite.
Now, when I click on it, I get to the php/make_favorite.php
file.
But what I want to do is, that the user does not see the php-file, he should stay on the same page and see an error or success message.
How do I manage that?
Use Ajax (ansynchronous javascript requests):
1) Include e.g. the jQuery javascript library jQuery in your page like so:
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
2) Add the javascript function to request your php/make_favorite.php file
<script type='text/javascript'>
function addFavourite(data1, data2)
{
$.ajax({
type: "POST",
url: "php/make_favorite.php",
data: {var1: data1, var2: data2},
success: function(msg){
alert( "Favourite remembered!: " + msg ); //Anything you want
}
});
}
</script>
3) Add the function to the onClick event of your link
<a href="#" onClick="addFavourite('image_id', 'some_data')" title="sn" alt=" Favorit " target="_self">...