HTML form with multiple submit parameters
I am trying to create a small web application that is used to remove items from a MySQL table. It just shows the elements in the HTML table and for each element a button [delete]
:
item_1 [delete]
item_2 [delete]
...
item_N [delete]
For this I am dynamically generating a table via PHP into an HTML form. Then this form obviously has N
[delete]
-buttons. The form must use the POST method to submit data.
For deletion, I wanted to send ID
(primary key in MySQL table) of corresponding item to php skript executable. Therefore, I introduced fields hidden
(all of these fields have name='ID'
that store the ID
corresponding element.
However, when you click the custom, [delete]
it always only returns the last one ID
(i.e. the value of the last hidden field ID
).
Is there a way to only submit the field of the ID
corresponding element without using multiple forms? Or is it possible to submit data from multiple forms with only one submit button? Or should I choose any other way?
The reason why I only want to do this in one form is that there are some "global" parameters that shouldn't be placed next to every element, but only once for the entire table.
a source to share
It seems to always return only the last id
It sends all of them, but since the name doesn't end with []
, PHP drops all of them to the last one.
Is there a way to submit just the ID field of the corresponding element without using multiple forms?
No. At least not without some bad JavaScript. All (not disabled) hidden inputs (with names and values) will succeed. You cannot limit based on proximity to the clicked input element.
If I understand your goals correctly, you have two main options.
- Place one shape per row (in a cell with a delete button)
- Encode id value in submit button name
a source to share
You can get rid of the hidden fields and name your submit buttons like this:
<input type="submit" name="delete[1]" />
<input type="submit" name="delete[2]" />
<input type="submit" name="delete[3]" />
and then
<?php
if (isset($_POST['delete'])) {
$toDeleteId = key($_POST['delete']);
}
a source to share