3

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.

2
  • You cannot run a PHP function from a click on a browser object. You ether need to write a javascript function called addfavourite() to call the PHP script or make the href="function.php" Commented Sep 25, 2015 at 11:06
  • This cannot work, you need to look into how JavaScript and PHP work. JavaScript is client-sided and has no visibility of the functions of PHP which is server-sided. onClick="addfavourite()" will call a potential addfavourite function 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. Commented Sep 25, 2015 at 11:07

2 Answers 2

1

make a javascript function and call the php page having the function addfavourite() with the help of AJAX. When the response is received as true, you can change the color of the button or star to yellow.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you! Could you show me and example, please? I've never used AJAX code and I've no idea... I want to make a product favourite and insert into my database clicking a yellow star, so I need when onClick event makes me this action.
1

I see what you are trying to do. What you have just done there is a combination of javascript and php however it is not the right way. Remember that javascript works with the front-end (browser and interface) and php is a back-end language. so you need a medium for them to communicate. I recommended that you use jquery/ json for this and assign an to the front-end. Check this:

infoproduct.php

<form action="infoproduct.php" method="POST">
<li><a href="#" name="id" onClick="addfavourite()"><img src="img/favstar.png" width="32" height="32"></a></li>

</form>

OR

infoproduct.php

  <li><a href="#" id="id" onClick="addfavourite()"><img src="img/favstar.png" width="32" height="32"></a></li>


<script>
$(document).ready(function(){

$('#id').click(function(){
var id=$(this).val();

$.post('function.php',{id:id},function(data)
{
 alert(successfully)
})


});

});
</script> 

**function.php**

    <?php

    //Calling the function 
    addfavourite();

    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);


    }

    ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.