Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

346
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda