开发者

HTML form with multiple submit options

I'm trying to create a small web app that is used to remove items from a MySQL table. It just shows the items in a HTML table and for each item a button [delete]:

item_1 [delete]
item_2 [delete]
...
item_N [delete]

To achieve this, I dynamically generate the table via PHP into a HTML form. This form has then obviously N [delete]-buttons. The form should use the POST-method for t开发者_JAVA技巧ransfering data.

For the deletion I wanted to submit the ID (primary key in the MySQL table) of the corresponding item to the executing php skript. So I introduced hidden fields (all these fields have the name='ID' that store the ID of the corresponding item.

However, when pressing an arbitrary [delete], it seems to submit always just the last ID (i.e. the value of the last ID hidden field).

Is there any way to submit just the ID field of the corresponding item without using multiple forms? Or is it possible to submit data from multiple forms with just one submit-button? Or should I even choose any completly different way?

The point why I want to do it in just one single form is that there are some "global" parameters that shall not be placed next to each item, but just once for the whole table.


<input type="submit" name="delete[1]" value="delete">

if (isset($_POST['delete'])) $id=key($_POST['delete']);


it seems to submit always just the last ID

It submits all of them, but since the name doesn't end with [], PHP discards all by the last.

Is there any way to submit just the ID field of the corresponding item without using multiple forms?

No. At least not without some unfortunate JavaScript. All (non-disabled) hidden inputs (with names and values) will be successful. You can't limit based on proximity to a clicked input element.

If I understand your goals correctly, you have two main options.

  1. Put one form per row (in the cell with the delete button)
  2. Encode the id value into the name of the submit button


You could 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']);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜