Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

php

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.";
}
like image 292
B.B10 Avatar asked Nov 22 '12 15:11

B.B10


1 Answers

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.

like image 164
Ivan Pintar Avatar answered Nov 14 '22 21:11

Ivan Pintar