带有多个提交选项的HTML表单

带有多个提交选项的HTML表单

问题描述:

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 transfering 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.

我正在尝试创建一个用于从MySQL表中删除项目的小型Web应用程序。 它只显示HTML表格中的项目,并为每个项目显示一个按钮 [delete] code>: p>

  item_1 [delete] 
item_2 [delete] \  n ... 
item_N [delete] 
  code>  pre> 
 
 

为实现这一目标,我通过PHP动态生成表格为HTML表单。 这个表格显然是 N code> [delete] code> -buttons。 表单应使用POST方法传输数据。 p>

对于删除,我想将相应项的 ID code>(MySQL表中的主键)提交给执行的php skript。 所以我介绍了 hidden code>字段(所有这些字段都有 name ='ID' code>,用于存储相应项目的 ID code>。 p>

但是,当按下任意 [delete] code>时,似乎总是只提交最后的 ID code>(即最后的值> ID code>隐藏字段。。 p>

有没有办法只提交相应项目的 ID code>字段而不使用多个表格?或者是否可以 只用一个提交按钮从多个表单提交数据?或者我是否应该选择任何完全不同的方式? p>

我想在一个表单中执行此操作的重点是那里 是一些“全局”参数,不应放在每个项目的旁边,而是整个表格只放一次。 p> div>

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