-4

From this code.

When I click the button, There alert only the first record.

assume that I have 5 records 5 buttons, Each record's value = 3,4,5,6,7.

It's alert only first button value = 3, for another button when I clicked it not alert and not work.

            <?php while($data = mysql_fetch_array($qr)) {  ?>
            <tr>
                <td>
                <button id="btn" value="<?=$data['id']?>">ff</button>
                </td>
                <td><?=$data['schoolof']?></td>
                <td><?=$data['major']?></td>
                <td><?=$data['firstname'].' '.$data['lastname']?></td>
            </tr>

            <?php } ?>
            <script>
                $("#btn").click(function () {
                    alert($("#btn").val());
                });
            </script>
Anto S
  • 2,448
  • 6
  • 32
  • 50

1 Answers1

4

You can have only one object with the same id in the same page. YOur code is inserting id="btn" for each button. Try to change your code to this:

        <?php while($data = mysql_fetch_array($qr)) {  ?>
        <tr>
            <td>
            <button class="btn" value="<?=$data['id']?>">ff</button>
            </td>
            <td><?=$data['schoolof']?></td>
            <td><?=$data['major']?></td>
            <td><?=$data['firstname'].' '.$data['lastname']?></td>
        </tr>

        <?php } ?>
        <script>
            $(".btn").click(function () {
                alert($(this).val());
            });
        </script>
Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74