0

This is my code

<td> <input type="submit" id="enviar" name="enviar" value="Calificar" class="btn btn-primary"> </input> </td>

I'm using this to send my form to another page called prueba_dos.php

$(document).on('ready',function(){

    $('#enviar').click(function(){
        event.preventDefault();
        var url = "prueba_dos.php";                                      

        $.ajax({                        
            type: "POST",                 
            url: url,                    
            data: $("#formulario").serialize(),
            success: function(data)            
            {
                $('#resp').html(data);           
            }
        });
    });
});
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 3
    You already know which button was clicked, as your code has `$('#enviar').click(...)`. As `id`s are unique there will only be one element with that `id`. Now, if you were doing `$(.enviar).click(...)` (ie. class), then you might need to find the corresponding `id`. – Sean Oct 13 '17 at 21:21
  • IDs on elements should be unique. So you should be able to use the unique id to tell which button was clicked. If your id's are not unique, then you have a coding flaw. – IncredibleHat Oct 13 '17 at 21:22
  • Heh @Sean, beat me to it ;) – IncredibleHat Oct 13 '17 at 21:22
  • Not side stepping the duplicate id issue pointed out, but if you do change it to be a class instead, 'this' will reference element the event is being processed against at the time inside the event handler. Optionally you can pass in the event and use `e.target`. – Taplar Oct 13 '17 at 21:25
  • well, I know wich button was cliked but I just have one button with id = "enviar" and name = "enviar". that button is repeat in loop, this way: – Jorge Luis Villanueva Atrisco Oct 13 '17 at 21:26
  • while ($mostrar=mysqli_fetch_array($resul)) { – Jorge Luis Villanueva Atrisco Oct 13 '17 at 21:26
  • 1
    Possible duplicate of [Does ID have to be unique in the whole page?](https://stackoverflow.com/questions/9454645/does-id-have-to-be-unique-in-the-whole-page) – Taplar Oct 13 '17 at 21:31
  • having a hardcoded `id="enviar"` in a loop is like giving everyone the same social security number, same cell phone number, or same license plate number. How are you going to tell the difference? Your `id`s need to be unique – Sean Oct 13 '17 at 21:31
  • I have just one button with id = "enviar" and name = "enviar". – Jorge Luis Villanueva Atrisco Oct 13 '17 at 21:37
  • I found this $(document).ready(function() { $( "form" ).submit(function () { // Get the submit button element var btn = $(this).find("input[type=submit]:focus" ); }); } – Jorge Luis Villanueva Atrisco Oct 13 '17 at 21:38
  • if you [have just one button](https://stackoverflow.com/questions/46738160/how-identify-wich-button-was-clicked-if-that-button-in-and-loopwhile-and-has#comment80423620_46738160) then you already know which button was clicked. 1 button = only button clicked. – Sean Oct 13 '17 at 21:42
  • my button is in loop(while),then my button is replicate in times, for that I need know which button was clicked. – Jorge Luis Villanueva Atrisco Oct 13 '17 at 21:47
  • If your button is replicated in a loop than you have multiple buttons with the same `id`. That is part of your issue. You need to fix that before you can move on. – Sean Oct 13 '17 at 21:50
  • 1
    headmax has shown below how you'd know which one was clicked. yet you keep avoiding the duplicate id issue we have been telling you about. there's really no new information we can give you at this point to address your questions. – Taplar Oct 13 '17 at 21:51

1 Answers1

1

Just like this you return own id or name as you want but you targeting all of them by using class:

$('.buttons').click(function(){
      console.log($(this).attr('id')); //id button clicked
      console.log($(this).attr('name')); //name button clicked
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<button id="test_id" name="test" class="buttons">test</button>
<button id="test_id1" name="test1" class="buttons">test1</button>
<button id="test_id2" name="test2" class="buttons">test2</button>
<button id="test_id3" name="test3" class="buttons">test3</button>
<button id="test_id4" name="test4" class="buttons">test4</button>