Skip to content
Advertisement

How to get id of submit type button, when button is pressed

I want to print out the id of the button I pressed. The id is set dynamically for each button in a table. This is my HTML code:

    echo '<td><center><input type="submit" id="'.$row['I_ID'].'" class="btn"
name="Add" value ="Add to cart"><center></td><tr>';

And I want to print the id of the button here.

if (isset($_POST['Add'])) {
    $ID = $_GET['id'];
    echo $ID;
    echo '<br/>' . "* The item has been added to your cart.";
}

Advertisement

Answer

If you do not wish to use a hidden field, you could set your submit button like this:

<button type="submit" name="id" value="value">Submit</button>

Then you can catch it with $_GET['id'].

You can have many buttons of type="submit" with the same name (id), and the value will be the one which was clicked, or the first one, if the form was submitted by pressing enter.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement