Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

345
Views
AJAX hover tooltip displays returned data only for first row in table

I am sending data to a PHP page using AJAX, where the data is fetched from MySQL. Hovering over the .payoudata class sends its data-id value to PHP, and gets the result. It is displayed using <div id="PayoutData"> but it's only displaying results related to the first record.

Here is my PHP code for generating the table:

<? while($srow = $stm->fetch(PDO::FETCH_ASSOC)) { ?>
  <tr>
    <td id="tooltip1">
      <a href="#" class="payoudata" data-id="<?php echo $srow['application_no']?>"><?php echo $srow['application_no']?>
       <span><div id="PayoutData"></div></span>
      </a>
    </td>
  </tr>  
<? } ?>

and here's my jQuery code for fetching and displaying the tooltip:

$('.payoudata').hover(function(){

    var paydata =  $(this).attr("data-id");

    $.ajax({
        type:'post',
        url:'payout-emp-data.php',
        data:{paydata : paydata},
        success: function(paydataresult){
            $('#PayoutData').html(paydataresult);
            }
        });
    });

I am getting first record in this <div id="PayoutData">

When I try to see the second record it's sending data and getting a result but unable to display it, as shown in the screenshots below. What might be a potential way of displaying the returned data?

Screenshot of first record:

Screenshot

Screenshot of second record:

Screenshot

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You generate a div with same id (PayoutData) for each row as id attribute should be unique. $('#PayoutData').html(paydataresult) will always get first one.

Try this for PHP:

<?php while($srow = $stm->fetch(PDO::FETCH_ASSOC)){ 
         $rowId = $srow['application_no'];
?>                
   <tr>
      <td id="tooltip1">
         <a href="#" class="payoudata" data-id="<?= $rowId ?>"><?= $rowId ?>
           <span>
             <div id="PayoutData<?= $rowId ?>"></div>
           </span>
         </a>
      </td>
    </tr>  
<? } ?>

And Javascript :

$('.payoudata').hover(function(){

  var paydata =  $(this).attr("data-id");

  $.ajax({
    type:'post',
    url:'payout-emp-data.php',
    data:{paydata : paydata},
    success: function(paydataresult){
        $('#PayoutData' + paydata).html(paydataresult);
    }
  });
});
about 4 years ago · Santiago Trujillo Report

0

HTML4 Specification says the ID must be unique.

The id attribute assigns a unique identifier to an element (which may be verified by an SGML parser).

to solve this issue you will need to use classes rather than ids

replace this:

$('#PayoutData').html(paydataresult);

by:

$('.PayoutData', $(this)).html(paydataresult);

and this line:

<div id="PayoutData">

by:

<div class="PayoutData">
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!