-3

I write a simple script with php and mysql for rating of products on my website when I click on Like link it send product id through ajax and get total number of likes of products. But when I click on Like link page redirect to page.php but I want that my index page not redirect to page.php but product id send to page.php. I use this simple html tag. Please help to resolving this issue.

<a href="page.php?id=ABC">Like</a>
<div class="likediv"></div>

AJAX/JQUERY

$(document).ready(function(){
  var catValue = "ABC";
  $.ajax({
   type:"post",
   url:"ajax_likedproduct.php",
   data:"catValue="+catValue,
   success: function(data){
     $('.likediv').text(data);
   }
  });
});

PHP

if(isset($_POST['catValue'])) {
   $data = $_POST['catValue'];
   $sql = mysql_query("SELECT SUM(likes) AS TotalLike FROM likes WHERE     product_id='".$data."';");
while($likes = mysql_fetch_assoc($sql)){
    if($likes['TotalLike'] == 0 || $likes['TotalLike'] == ""){
      echo'0';
    }else{
        echo $likes['TotalLike'];
    }
}
}
j08691
  • 204,283
  • 31
  • 260
  • 272

1 Answers1

0

Have the full link you want in the original anchor href and decorate the desired links with a common class (for easier access):

<a href="ajax_likedproduct.php?id=ABC" class="like">Like</a>
<div class="likediv"></div>

and code like this:

// Use a single delegated handler for all like links
$(document).on('click', 'a.like', function(e){
   // Stop the normal click behavior
   e. preventDefault();
   // Use the existing URL for progressive enhancement
   var url = $(this).attr("href");
   $.ajax({
      type:"post",
      url: url,
      success: function(data){
        $('.likediv').text(data);
      }
   });
});
iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202