/    Sign up×
Community /Pin to ProfileBookmark

Like button only works for one single post

So I’m trying to implement a like button for users’ posts. I’ve got the button working so that when clicked, the number of likes goes up by one, and the like button image changes with ajax. The problem is that the like button only works for ‘one’ post on index.php instead of working for every post. How can I fix my code so that each like button on each different post (when clicked) works properly? Like I said, the first post works properly, but the others don’t. Here is the code :

[CODE]<!doctype html>
<head>

<?php
include(‘header.php’);
?>

<script>
$(document).ready(function(){
$(“#img_like”).click(function(){
$.ajax({url:”liked_button.php”,success:function(result){
$(“#like_system”).html(result);
}});
});
});
</script>

<script>
function increase(){
var total_likes = document.getElementById(“like_total”);
total_likes.value++;
}
</script>

</head>
<link type=”text/css” rel=”stylesheet” href=”index.css”>
<body>

<?php

$conn = mysqli_connect(“localhost”,”root”,””) or die (“No SQLI”);
mysqli_select_db($conn, “sample”) or die (“No DB”);

$sqli = “SELECT * FROM `photos` ORDER BY `id` DESC”;

$result = mysqli_query($conn, $sqli) or die (“No query”);

while($row = mysqli_fetch_assoc($result))

{
$username = $row[‘username’];
$title = $row[‘title’];
$description = $row[‘description’];
$image_name = $row[‘image_name’];
$image_id = $row[‘image_id’];
$random_directory = $row[‘random_direc’];
$date = $row[‘post_date’];

$image_info = “http://localhost/splindr_2.0/photos/$random_directory/$image_id”;

echo “<div id=contentWrapper’>
<div id=’photo’>
<div id=’actual_image’>
<img src=’$image_info’>
</div>
<div id=’like_system’><img id=’img_like’ onClick=’increase()’ src=’https://i.imgur.com/pprs4Ch.jpg’></div>
<input type=’text’ name=’like_total’ id=’like_total’ value=’0′>
<div id=’info_wrapper’>
<div id=’info_header’>Title: $title &nbsp By: $username &nbsp Date: $date</div>
<div id=’description’>$description</div>
</div>
</div>
</div>”;//end contentWrapper

}

?>

</body>
</html>[/CODE]

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @ralston3 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...