简体   繁体   中英

ajax not working for LIKE Button

I'm having some issues with AJAX/JS and PHP. What i'm trying to do is a LIKE button, when if clicked, the page is not reloaded, but the like column for a particular comment is updated. I have being able to come up with these two scripts php and AJAX. but as it is now nothing shows up on the screen when i load about_cs.php and click the LIKE Button. Please where could my problem be coming from? Thanks for your time and patience, I most appreciate it. Thank you.

about_cs.php

    $q = "SELECT c.comment_id
    FROM comment AS c  
    INNER JOIN about AS ac ON
    c.article_id = ac.about_id
    WHERE   c.article_id =  '".$article_id."'
    AND page_name = '".$page_name."'"
    or die (mysql_error());



    $r = mysql_query($q);
    if(mysql_num_rows($r)==1)
    {

   $row = mysql_fetch_assoc($r);
    $likes = $row['votes_up'];
    }

    $comment_id = $_GET['comment_id'];
    $action = $_GET['action'];


    if($action=='like') 
    {
    $total_likes = $likes+1;
    $q = "UPDATE comment 
    SET like = $total_likes 
    WHERE   c.article_id =  '".$article_id."'
    AND page_name = '".$page_name."'";
    }

LIKE Button

echo "<a class=\"like\" href=\"about_cs.       php?action=like&comment_id=
  $comment[comment_id]&article_id=$_
  SESSION[article_id] \"><img src =\"like.
  jpeg\" ></a>";


 $(function(){
 $(".like").click(function(){
 the_id = $(this).attr('comment_id');
    $.ajax({
      type: "GET",
            data: "action=likep&comment_id     ="+$(this).attr("comment_id"),
        url: "about_cs.php",
        success: function(msg)
        {
        $("span.likes"+the_id).html(msg);  


        }
       });
      });
      });

You are adding class to the anchor and trying to target it by the id selector.

$(".like").click(function(){

   ^ notice the selector. 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM