简体   繁体   English

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

[英]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. 我正在尝试创建一个用于从MySQL表中删除项目的小型Web应用程序。 It just shows the items in a HTML table and for each item a button [delete] : 它只显示HTML表格中的项目,并为每个项目显示一个按钮[delete]

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

To achieve this, I dynamically generate the table via PHP into a HTML form. 为此,我通过PHP动态生成表格为HTML表单。 This form has then obviously N [delete] -buttons. 这个表格显然有N [delete]按钮。 The form should use the POST-method for transfering data. 表单应使用POST方法传输数据。

For the deletion I wanted to submit the ID (primary key in the MySQL table) of the corresponding item to the executing php skript. 对于删除,我想将相应项的ID (MySQL表中的主键)提交给执行的php skript。 So I introduced hidden fields (all these fields have the name='ID' that store the ID of the corresponding item. 所以我介绍了hidden字段(所有这些字段都有name='ID' ,用于存储相应项目的ID

However, when pressing an arbitrary [delete] , it seems to submit always just the last ID (ie the value of the last ID hidden field). 但是,当按下任意[delete] ,似乎总是只提交最后一个ID (即最后一个ID隐藏字段的值)。

Is there any way to submit just the ID field of the corresponding item without using multiple forms? 有没有办法只提交相应项目的ID字段而不使用多个表格? 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 它似乎总是提交最后一个ID

It submits all of them, but since the name doesn't end with [] , PHP discards all by the last. 它提交了所有这些,但由于名称不以[]结尾,因此PHP会丢弃所有内容。

Is there any way to submit just the ID field of the corresponding item without using multiple forms? 有没有办法只提交相应项目的ID字段而不使用多个表格?

No. At least not without some unfortunate JavaScript. 不,至少没有一些不幸的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 将id值编码到提交按钮的名称中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM