How could I do to make an onClick event in a href in an image? I've the following code:
- infoproduct.php (html code)
<li><a href="#" onClick="addfavourite()"><img src="img/favstar.png" width="32" height="32"></a></li>
- function.php
function addfavourite() {
$sql = "SELECT * FROM `users` WHERE `iduser` = '".$_SESSION['id']."'";
$result = mysql_query($sql, $db_connection);
$row = mysql_fetch_assoc($result);
$newfav = 'INSERT INTO favourites (`iduser`,`idproduct`) VALUES ("'.$row['iduser'].'","'.$_GET['IDp'].'");'; //IDp = ID product obtained from URL
$createfav = mysql_query($newfav, $db_connection);
}
When I click on the image it doesn't work and I can't include my favourite product in the database. $_SESSION & $_GET works correctly and functions.php is included in infoproduct.php
I've also tried to put in html code onClick="addfavourite();" but neither works.
addfavourite()to call the PHP script or make thehref="function.php"onClick="addfavourite()"will call a potentialaddfavouritefunction that is defined in JavaScript. If it isn't defined, then you are likely to see an error in your console. The only way to trigger PHP execution with JavaScript is to use Ajax. I invite you to read tutorials about it.