How can I get the data of a specific field from a tabl, by using php and js?

0

I have a problem when I try to get the id of a table here

    $salida.="<table class='table'>
            <thead>
                <tr id='titulo'>
                    <th scope='col'>#Contrato</th>
                    <th scope='col'>Nombre</th>
                    <th scope='col'>Fecha Contrato</th>
                    <th scope='col'>Direccion</th>
                    <th scope='col'>Colonia</th>
                    <th scope='col'>Tipo de Cuota</th>
                </tr>
            </thead>
            <tbody id='mytable'>";

    while ($fila = $resultado->fetch_assoc()) {
        $salida.="<tr style='cursor:pointer' onclick='al();'>    
                    <td class="id">".$fila['NUMECONTRA']."</td>
                    <td>".$fila['NOMBREUSER']."</td>
                    <td>".$fila['FECHACONTR']."</td>
                    <td>".$fila['DIRECCUSER']."</td>
                    <td>".$fila['COLONIAUSE']."</td>
                    <td>".$fila['TIPOCUOTAS']."</td>
                </tr>";

    }
    $salida.="</tbody>
                </table>";
}else{
    $salida.="NO HAY DATOS :(";
}


echo $salida;

and I want to be able to obtain the id data through js which I leave below.

$(document).ready(function() {
  $('tr').click(function() {
    alert($(this).find('td.id').text())
  });

I did a test on a sheet of HTML and it grabs me without problems.

<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>First Name</th>
            <th>Last Name</th>
        </tr>
    </thead>
    <tbody>
        <tr >
            <td class="id">45</td>
            <td>Bruce</td>
            <td>Almighty</td>
        </tr>
        <tr>
            <td class="id">22</td>
            <td>Bruce</td>
            <td>Evans</td>
        </tr>
  </tbody>
</table>

The point is that on the PHP sheet it does not grab me and I do not know what I'm doing wrong.

    
asked by Yayo Venegas 09.10.2018 в 18:24
source

2 answers

0

If you do not repeat your "id" identifier, you could change the code:

<td class="id">".$fila['NUMECONTRA']."</td>

by:

<td class="fila" id="[el id que le estas pasando]">".$fila['NUMECONTRA']."</td>

and in javascript:

alert($(this).find('.fila').attr('id')) });

    
answered by 09.10.2018 / 18:32
source
0

If you can not recover the value via PHP, javascript is another solution:

let ids = Array.from(document.querySelectorAll(".id"))

ids.forEach((id) =>{console.log(id.textContent)})
<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>First Name</th>
            <th>Last Name</th>
        </tr>
    </thead>
    <tbody>
        <tr >
            <td class="id">45</td>
            <td>Bruce</td>
            <td>Almighty</td>
        </tr>
        <tr>
            <td class="id">22</td>
            <td>Bruce</td>
            <td>Evans</td>
        </tr>
  </tbody>
</table>
    
answered by 09.10.2018 в 20:34