Stack Overflow Asked by Nancy Moore on November 7, 2021
This code display several records from database and its working fine.
Here is my issue: when I click Eg. on button 1 to send my like, the loading text
Please Wait, sending like…
is seen displayed all over the record instead of displaying just for the records that I clicked.
The same thing is applicable when like result is being retrieved. Here is the screenshot:
I think I need to add record id to loader and result_like.
Something like
'loader'+id
'result_like'+id
Here is the entire code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
$('.like_post').click(function () {
var id = $(this).data('id');
alert(id);
$('.loader').fadeIn(400).html('<div style="background:green;color:white;padding:6px;">Please Wait, sending like...</div>');
var rec = {id: id};
$.ajax({
type: 'POST',
url: 'like_rec.php',
data: rec,
cache: false,
success: function (msg) {
$('.loader').hide();
$('.result_like').html(msg);
}
});
});
});
</script>
<?php
echo "<center><h3>Like Records</h3></center>";
include('db.php');
$res = $db->prepare("SELECT * FROM records_data");
$res->execute(array());
while ($row = $res->fetch()) {
$id = htmlentities(htmlentities($row['id'], ENT_QUOTES, "UTF-8"));
$file_name = $row['myfile'];
?>
<div style="color:white;background:blue;padding:10px;">
<b> Id:</b> <?php echo $id; ?><br>
<b> Files Name:</b> <?php echo $file_name; ?><br>
<div class="loader"></div>
<div class="result_like"></div>
<button data-id="<?php echo $id; ?>" class="like_post">like Record(<?php echo $id; ?>)</button>
<br>
</div>
<?php
}
?>
like_rec.php
<?php
$id =$_POST['id'];
echo "I liked File with ID: ($id)";
?>
You're currently targeting every "loader" and every "result_like":
$('.loader').hide();
$('.result_like').html(msg);
Since the elements you want to target are all contained within a single parent <div>
, you can use that for jQuery DOM traversal. Starting from the element being clicked, you can navigate to the containing <div>
and then back down to the target element(s). Something like this:
$(this).parent('div').find('.loader').hide();
$(this).parent('div').find('.result_like').html(msg);
To be repeated anywhere you want to target an element within that "group" of elements. You could also store the containing <div>
in a variable if you prefer, which comes with a tiny performance improvement:
var container = $(this).parent('div');
//...
container.find('.loader').hide();
container.find('.result_like').html(msg);
Answered by David on November 7, 2021
You need to get the loader in relation to the clicked button so instead of using
$('.loader')
Use
$(this).parent().find('.loader')
Same goes for your result message.
The following is a modified click function:
$('.like_post').click(function(e) {
// as you are doing an ajax post here, I would disable the default action of the button:
e.preventDefault();
var $button = $(this); // you use this multiple times so it is more efficient to put it in a var
var id = $button.data('id');
var $buttParent = $button.parent();
var $loader = $buttonParent.find('.loader');
$loader.html('<div style="background:green;color:white;padding:6px;">Please Wait, sending like...</div>').fadeIn(400); // I would change the html before the fadeIn
var rec = {
id: id
};
$.ajax({
type: 'POST',
url: 'like_rec.php',
data: rec,
cache: false,
success: function(msg) {
$loader.hide();
$buttonParent.find('.result_like').html(msg);
}
});
});
Also the center
tag is obsolete so don't use it, instead give your heading a class and use css to centre the text
Answered by Pete on November 7, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP