Grab button id pressed

Asked

Viewed 2,849 times

1

How can I get the id of the button pressed, and the buttons are automatically generated with php and do not have your id fixed, I need the id of the button to make an SQL query with php,

Code that generates the button:

 while ($linha = mysqli_fetch_array($ids))
              {
                  $query = ("select * from componentes where id_principal = ".$linha[0]." ");
                  $componentes = mysqli_query($con,$query);
                  $resultado = mysqli_fetch_row($componentes);

                  $id_componente = $resultado[0];
                  $codigo = $resultado[2];
                  $nome = $resultado[3];
                  $entrada = $resultado[4];
                  $saida = $resultado[5];
                  $f = $resultado[6];
                  $m = $resultado[7];
                  $g = $resultado[8];
                  $gg = $resultado[9];
                  $total = ($f + $m + $g + $gg);

                  print "<tr>";
                  // ESSE É O BOTÃO QUE PRECISO PEGAR O ID QUANDO ELE SER PRESSIONADO
                  print "<td><button id=".$id_componente." type='submit' class='btn btn-success'>".$codigo."</td>";

3 answers

3


You can use a class of generated elements, for example

class='btn btn-Success test'

and then:

$(".teste").click(function()
{
    var id = $(this).attr('id');
});
  • Only with ajax to pass this value to php?

  • directly yes, or you could use a less conventional way, create a Hidden input, fill in the value of that id and give a Submit in the form

  • Could I relate this Hidden input to the button that was pressed?

  • $("input[name=input_test]"). val(id);

  • with that code within the function mentioned above you already give the id value for the input value

  • And do I need to create input or does this jquery code already create it? after submitting the form with post, I get the php value right : $_POST['input_teste']

  • Very good your answer, congratulations.

  • Exactly, you create the input and then take the value as you mentioned. Thank you, I’m happy to help!

Show 3 more comments

2

Add a class and put the event in it to catch the id button:

<?
 while ($linha = mysqli_fetch_array($ids))
              {
                  $query = ("select * from componentes where id_principal = ".$linha[0]." ");
                  $componentes = mysqli_query($con,$query);
                  $resultado = mysqli_fetch_row($componentes);

                  $id_componente = $resultado[0];
                  $codigo = $resultado[2];
                  $nome = $resultado[3];
                  $entrada = $resultado[4];
                  $saida = $resultado[5];
                  $f = $resultado[6];
                  $m = $resultado[7];
                  $g = $resultado[8];
                  $gg = $resultado[9];
                  $total = ($f + $m + $g + $gg);

                  print "<tr>";
                  // ESSE É O BOTÃO QUE PRECISO PEGAR O ID QUANDO ELE SER PRESSIONADO
                  print "<td><button id=".$id_componente." type='submit' class='btn btn-success bt-componente'>".$codigo."</td>";

?>

<script>
    $(function(){
        $('.bt-componente').click(function(){
             var id = $(this).attr('id'); //aqui você tem o id para usar em um ajax ou algo do tipo...
             $.ajax({
                 url: "urlquefaraaconsulta",
                 type: "post", //opcional, padrão get
                 data: {id: id},
                 success: function(data){
                     $('table').after(data); //exemplo de como inserir o conetúdo retornado
                 }
             });
        });
    });
</script>

Another alternative would be to redirect the user directly to a screen that will make the query using the id as a parameter:

<script>
$(function(){
    $('.bt-componente').click(function(){
         var id = $(this).attr('id');
         window.location.href = "minhaurl?id=" + id;
    });
});
</script>
  • Only with ajax to pass this value to php?

  • Using jQuery yes, it has some ways to do but the concept is the same...I’ll add an example

  • Okay, thank you

  • At the time of inserting there are several ways to do, there will depend on where in your html will insert the return as it will return...

  • I added one more suggestion.

1

You can use the function attr jQuery to get the attribute value:

$("button").click(function(){
  console.log($(this).attr("id"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="botao1">Botão</button>
<button id="botao2">Botão</button>

  • Only with ajax to pass this value to php?

  • Depends @Robertoalbino. With Ajax tends to get more performative by not giving refresh on the screen.

  • I get it, it’s because I have some sync problems using ajax, I can never get the value in the moment I need.

  • What would be these problems?

  • The query is invalid for not having the ajax value, and after it has been executed I get the ajax value.

  • "The query is invalid because it does not have the value of ajax", how so? And it is normal to receive the value after executed

  • At the moment when php makes the query sql the value of the variable is empty, only after the query has been executed, is that the value of the variable is filled by ajax, a synchronization problem between my php code and ajax.

Show 2 more comments

Browser other questions tagged

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