P murad P murad - 1 year ago 53
PHP Question

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");
type: "GET",
url: '../connect.php',
data: "I=" + i,
success: function(data)


if (isset($_GET["I"])) {

$query3=$con->query("SELECT id,likes FROM uploads WHERE Rand='$RandS'");

$Sel2=$con->query("SELECT id FROM likes WHERE User_id='$NameId'");
if ($num_rows>0) {
echo $Likes;
$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


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;