P murad
P murad

Reputation: 39

AJAX works on second click

I have got this in my feed.php

    <div class="Likes" data-i=<?php echo $row[8];?>>
<img src="../img/like.png"><p class="L_c"><?php echo $row[4];?></p></div>

And this in my jquery.js

$(".Likes").on('click',function() {
        var i=$(this).attr("data-i");
                $.ajax({
                    type: "GET",
                    url: '../connect.php',
                    data: "I=" + i,
                    success: function(data)
                    {
                         $(".L_c").html(data);
                    }
                });
        });

connect.php

if (isset($_GET["I"])) {
        $RandS=$_GET["I"];

        $query3=$con->query("SELECT id,likes FROM uploads WHERE Rand='$RandS'");
        $row=$query3->fetch_row();
        $IdU=$row[0];
        $Likes=$row[1];

        $Sel2=$con->query("SELECT id FROM likes WHERE User_id='$NameId'");
        $num_rows=$Sel2->num_rows;
        if ($num_rows>0) {
            echo $Likes;
        }else{
                $query=$con->query("INSERT INTO likes (Post_id,User_id,`DATE`) VALUES('$IdU','$NameId',NOW())");
                $query=$con->query("UPDATE uploads SET Likes=Likes+1 WHERE Rand='$RandS'");
                echo $Likes;
        }
    }

But when i click for the first time nothing happens it only adds 1 on second time

Upvotes: 1

Views: 148

Answers (1)

Maths RkBala
Maths RkBala

Reputation: 2195

Everything run smoothly, you missed the logic in php

update the $Likes + 1

but echo $Likes

More over $Likes not update or insert the following:

if ($num_rows>0) {
   echo $Likes;
}

Upvotes: 1

Related Questions