How to take the value of a TD?

Asked

Viewed 1,876 times

1

I tried several ways (SEVERAL SAME) but did not succeed! Most return me one UNDEFINED

Follows the code:

PHP:

<?php

 require_once('acessabanco.php');

 $objDb = new db();
 $link = $objDb->conecta_banco();

 $sql = "SELECT p.sequencia codigo,
                p.nome_completo nome, 
                c.data_nascimento data, 
                t.celular_1 celular
           FROM pessoas p, 
                clientes c, 
                telefones t 
          WHERE p.sequencia = t.cod_pessoa 
            AND c.cod_pessoa = p.sequencia";
 $exec = mysqli_query($link, $sql);     
 if ($exec){
    while ($clientes = mysqli_fetch_array($exec)){
        echo('<tr><td id="sequencia">' . $clientes['codigo'] . '</td>
              <td>' . $clientes['nome'] . '</td>
              <td>' . $clientes['data'] . '</td>
              <td>' . $clientes['celular'] . '</td>
              <td><button type="button" id="teste" class="btn btn-primary btn-sm">Small button</button></td>
              <td><a type="button" onClick="teste()" class="btn btn-danger btn-sm">Small button</a></td></tr>');
    }
}
?>

JQUERY:

$(document).ready(function () { 

$('#content').css('display', 'none');

$.ajax({
    url: 'php/lista_clientes.php',
    type: 'post',
    success: function (data) {
        $("#carrega_pag").append(data);
    }
});
function teste (){  

$('table tr').each(function(){
    var teste = $(this).closest('td').attr('id');
console.log(teste);
)};

HTML:

    <table class="table table-hover">
  <thead>
    <tr>
      <th>Código</th>
      <th>Nome Completo</th>
      <th>Data de Nascimento</th>
      <th>Celular</th>
      <th>Opções</th>
    </tr>
  </thead>
  <tbody id ="carrega_pag">
  </tbody>
</table>

Imagery: inserir a descrição da imagem aqui

Could someone help me? I am very grateful! Because I have been here for a long time... Thank you!!

2 answers

0


Try it like this:

PHP:

<?php

 require_once('acessabanco.php');

 $objDb = new db();
 $link = $objDb->conecta_banco();

 $sql = "SELECT p.sequencia codigo,
                p.nome_completo nome, 
                c.data_nascimento data, 
                t.celular_1 celular
           FROM pessoas p, 
                clientes c, 
                telefones t 
          WHERE p.sequencia = t.cod_pessoa 
            AND c.cod_pessoa = p.sequencia";
 $exec = mysqli_query($link, $sql);     
 if ($exec){
    while ($clientes = mysqli_fetch_array($exec)){
        echo('<tr><td id="sequencia" >' . $clientes['codigo'] . '</td>
              <td>' . $clientes['nome'] . '</td>
              <td>' . $clientes['data'] . '</td>
              <td>' . $clientes['celular'] . '</td>
              <td><button type="button"  id="'.$clientes["codigo"].'" class="btn_sequencia btn btn-primary btn-sm">Small button</button></td>
              </tr>');
    }
}
?>

jQuery:

$(document).ready(function () { 

    $(".btn_sequencia").on('click', function(){
        alert($(this).attr('id'));
    });

    $('#content').css('display', 'none');

    $.ajax({
        url: 'php/lista_clientes.php',
        type: 'post',
        success: function (data) {
            $("#carrega_pag").append(data);
        }
    });

});

Test code:

$(document).ready(function () { 

    $(".btn_sequencia").on('click', function(){
        alert($(this).attr('id'));
    });

    $('#content').css('display', 'none');

    $.ajax({
        url: 'php/lista_clientes.php',
        type: 'post',
        success: function (data) {
            $("#carrega_pag").append(data);
        }
    });

});
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<table class="table table-hover">
    <thead>
        <tr>
            <th>Código</th>
            <th>Nome Completo</th>
            <th>Data de Nascimento</th>
            <th>Celular</th>
            <th>Opções</th>
        </tr>
    </thead>
    <tbody id ="carrega_pag">
        <tr>
            <td id="sequencia">1</td>
            <td>Joao</td>
            <td>11/11/2017</td>
            <td>99999-9999</td>
            <td><button type="button" id="1" class="btn_sequencia btn btn-primary btn-sm">Small button</button></td>
        </tr>
        <tr>
            <td id="sequencia">2</td>
            <td>Maria</td>
            <td>11/11/2017</td>
            <td>99999-9999</td>
            <td><button type="button" id="2" class="btn_sequencia btn btn-primary btn-sm">Small button</button></td>
        </tr>
    </tbody>
</table>

  • I lie, now he shows me nothing!

  • Darlei, thank you for answering! That way it does not show me anything, I had already tried to put the CLICK function, by the class or by the ID, but it does not display anything, nor an Alert.

  • I had an error in jquery, I hadn’t noticed either, I edited the answer, test now

  • Thank you for answering! I tried with this new code, what happens: The TD that we put fixed, displays correctly, however how I am mounting the TD in PHP and giving an append, maybe it does not recognize.. That is, your code worked on the TD we put fixed, but the ones I mount in PHP, does not display anything, the button is not recognized..

0

We need delegate the .on('click'... the new elements returned by Ajax, and use the selector $(this).closest('tr').find('td').first().text() to capture the text of the first td from the button line clicked.

The code would look like this:

$(document).ready(function () { 

   $('#carrega_pag').on('click',".btn_sequencia", function(){
        alert($(this).closest('tr').find('td').first().text().trim());
    });

   $('#content').css('display', 'none');

   $.ajax({
      url: 'conecta.php',
      type: 'post',
      success: function (data) {
         $("#carrega_pag").append(data);
      }
   });

});
  • DVD, thanks for responding! I tried this way, but it displays me the name of the id, not the content, in case it is displaying me "SEQUENCIA".

  • Thanks for the help! I need to pick up the text inside the TD, for example: I clicked on the first line button, it displays the contents of the TD SEQUENCIA (in this case it displays the code 49) and so, successively to the other items

  • @Joaopedro I updated the answer. You will get the contents of the first td by clicking on the last td button, where you have <a></a>

  • Orra, it worked, man! Thank you very much!! Now it worked with lace! No words to thank you for your help! Thank you very much! Credits: Dvd and João Pedro!

  • @Joaopedro Good! Be sure to check if you find it useful. Abs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.