Send id to script by ajax depending on the button that is chosen

0

I am trying to send emails to certain people depending on the button that is chosen, the table in the interface consists of two fields, the name of the person and the button that is going to send the mail when you click.

This table is built by means of a loop from an array extracted from a database previously containing the ids of the people.

echo '<legend>Enviar correos</legend>';
echo '<table>';
    while ($row = mysqli_fetch_assoc($result)) {
            echo '<tr>';
                echo '<td>'.ucwords(getNameEmp($row['employee'])).'</td>';
                echo '<td><button type="button" class="btn btn-default" name="send" value="'.$row['employee'].'" id="send"><span class="glyphicon glyphicon-send" aria-hidden="true"></span></button></td>';
            echo '</tr>';
    }
echo '</table>';

And my request for ajax is this:

      $("#send").click('submit',(function(e) {

            e.preventDefault();
            $.ajax({
                url: "sendMails.php",
                type: "POST",
                data:  { 
                    id: $(this).val()
                },
                success: function(result) {
                    alert('ok');
                },
                error: function(result) {
                    alert('error');
                }
            });
        }));

My question is, what is the best way to know which button was selected to know which person has to send the mail?

    
asked by Fernando Garcia 17.08.2018 в 00:08
source

2 answers

0

Try this way:

echo '<legend>Enviar correos</legend>';
echo '<table>';
    while ($row = mysqli_fetch_assoc($result)) {
            echo '<tr>';
                echo '<td>'.ucwords(getNameEmp($row['employee'])).'</td>';
                echo '<td><button type="button" class="btn btn-default email" name="send" data-id="'.$row['employee'].'" id="send"><span class="glyphicon glyphicon-send" aria-hidden="true"></span></button></td>';
            echo '</tr>';
    }
echo '</table>';

Then in ajax:

$(".email").click(function(e) {

            e.preventDefault();
            $.ajax({
                url: "sendMails.php",
                type: "POST",
                data:  { 
                    id: $(this).attr('data-id')
                },
                success: function(result) {
                    alert('ok');
                },
                error: function(result) {
                    alert('error');
                }
            });
        }));

To the button add the class email as selector for jquery and ajax and in ajax change the selector and add the take of the id in this way: $(this).attr('data-id')

    
answered by 17.08.2018 / 00:39
source
2

It is wrong to use the same id, since it should only be unique. In your case each row has the same id, "send", and you can not identify which button is the one that sends.

I suggest you delete the part of id="send" .

On the other hand, the javascript must locate each button independently. I recommend you change the javascript by:

   $("[name='send']").click('submit',(function(e) {
            value = $this.value();
            e.preventDefault();
            $.ajax({
                url: "sendMails.php",
                type: "POST",
                data:  { 
                    id: $(this).val()
                },
                success: function(result) {
                    alert('ok');
                },
                error: function(result) {
                    alert('error');
                }
            });
        }));

I have changed the selector so that it looks for all those with the name attribute the send value (in your case, all the buttons). On the other hand, the value will be $ this (the button you have clicked) and .value ().

    
answered by 17.08.2018 в 00:39