I've been developing a website similar to YouTube, and I'm facing difficulties with the Like/Dislike feature for comments.
Currently, I have implemented it in the following way: when a user clicks on an image (thumbsUp.png or thumbsDown.png), a JavaScript function is called. Below is the PHP code snippet where $enregistrement represents the comment:
$block .='<img src="'.APP_IMG_PATH.'thumbsup.png" onclick="javascript:addLike('.$enregistrement["Comm_ID"].')" style="width:20px;height:20px"></img> '.$enregistrement["Likes"].' '
. '<img src="'.APP_IMG_PATH.'thumbsdown.png" onclick="javascript:addDislike('.$enregistrement["Comm_ID"].')" style="width:20px;height:20px"></img>'.$enregistrement["Dislikes"].'';
Here are my JavaScript functions:
$block .='
<script>
function addlike(idCo){
$.ajax({
type: "GET",
url: "'.APP_SERVICE_PATH.'SetComment.php",
data: {Like:1, commID:idCo},
success: function(data){
},
error: function(exc){
alert("Exception: Une erreur a été levé sur $_GET de addLike(). " + exc);
}
)};
}
function addDislike(idCo){
$.ajax({
type: "GET",
url: "'.APP_SERVICE_PATH.'SetComment.php",
data: {Dislike:1, commID:idCo},
success: function(data){
},
error: function(exc){
alert("Exception: Une erreur a été levé sur $_GET de addDislike().");
}
)};
}
</script>';
However, I'm having an issue where the onclick event is not triggering at all! Previously, I had <a href="">
tags surrounding the <img>
tags which worked but caused page reloads upon each click, something I want to avoid.
If anyone can spot what I might be doing wrong, I would greatly appreciate any assistance.
Please note that I am aware of the need to separate PHP and HTML code, so please refrain from commenting on this aspect. Thank you!