How to make tables with BUTTONS with different ids for function javascript

1

Good afternoon
I'm setting up a table with the following code

<table align="center" cellpadding="10" id="user_table" class='table table-striped table-advance table-hover'>
<tr>
<th>NOME</th>
<th>C.P.F.</th>
<th>IDENTIDADE</th>
<th>TEL.</th>
<th>E-MAIL</th>

<th style='text-align: center'>MARCAR</th>
</tr>
<?php
while ($row=mysql_fetch_array($select)) 
{
 ?>
 <tr id="row<?php echo $row['id_paciente'];?>">
  <td > <?php echo $row['nome'] . " " . $data_consulta . " " . $id_m_h;?></td>
  <td > <?php echo $row['cpf'];?></td>
  <td > <?php echo $row['identidade'];?></td>
  <td > <?php echo $row['tel_01'];?></td>
  <td > <?php echo $row['email'];?></td>

  <td style='text-align: center'>       
    <button id="id_c02b_marcar_consulta" type="button"  class="btn"  value="<?php echo $row['id_paciente']?>" >MARCAR <?php echo $row['id_paciente']?></button>
  </td>
 </tr>
 <?php
}
?>
</table>

As you can see within WHILE you have the following line

<button id="id_c02b_marcar_consulta" type="button"  class="btn"  value="<?php echo $row['id_paciente']?>" >MARCAR <?php echo $row['id_paciente']?></button>

I usually use the following code in java script to perform an action

$('#id_c02b_marcar_consulta').click(function () 
            {               
            funcao_patati_patata($("#id_c02b_marcar_consulta").val(),"<?php $id_usuario; ?>","<?php $id_horario; ?>"); 
            });

As I am in a TABLE and the IDs ARE EQUAL ... the above code will only work for the first line. Home My idea is every BUTTON on the table line have to write a record containing user_id, patient_id, hour_id. I already have all the variables loaded.
I do not have much experience in PHP / JAVASCRIPT / JQUERY so I'm not seeing another path to my problem Do a javascript function that takes all the rows of the table
With my current knowledge I would have to have a javascript function for each button in the table row. It is not an option since the table is dynamic and comes from the database.

    
asked by anonymous 18.09.2017 / 20:59

2 answers

0

To trigger the same function on multiple elements they need to be identified by classnames.

No HTML:

   <button id="id_c02b_marcar_consulta" type="button"  class="id_c02b_marcar_consulta btn"  value="<?php echo $row['id_paciente']?>" >MARCAR <?php echo $row['id_paciente']?></button>

And in the event:

$('.id_c02b_marcar_consulta').click(function (){console.log('fui clicado')}) 
    
18.09.2017 / 21:03
0

I'll stick to the problem in your script.

The problem is that ID's should be unique on your page. Therefore, when querying a node by ID, only one value will be returned. This is where css classes come in. Classes can be repeated on their nodes, and return multiple values when queried.

So you could add a class to the button's class attribute, which would look like this:

<button type="button" class="btn marcar-consulta"></button>

With the class in position you could do something like:

$(".marcar-consulta").on("click", function(){
    funcao_que_marca_consulta($(this).val());
});

Q: I do not think I should mix PHP code with JavaScript code.

    
18.09.2017 / 21:58