Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

php - 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.";
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...