输入类型数组的 $_POST 值 =“image”大批?
我有一个从数据库中提取的动态项目列表,其中该列表包含 0 到 N 个项目。每个项目都在 HTML 表中列成一行,并带有提交按钮。我需要知道在 $_POST 上单击了这些按钮中的哪一个,并获取该项目的值。
我目前的方法是使用数组。但是,$_POST 值返回数组中的所有项目,而不仅仅是单击的项目。此外,数组值与从数据库中提取的值不相关。同一图像提交按钮可能会被重复单击并产生不同的值。
<?php
else if (isset($_POST["deleteItem"]))
{
foreach ($_POST["deleteItem"] as $value) :
WishList::Delete($value);
endforeach;
}
?>
<form method="post" action="">
<table id="WishListTable">
<?php
$wishlist = WishList::GetAllByID($userID);
foreach ($wishlist as $item)
{
echo "<tr><td>" . $item->Description . "</td>";
echo "<td>";
if ($item->InStock)
{
$primaryEmailAlreadyUsed = true;
echo "In Stock";
}
else {
echo "Out of Stock";
}
echo "</td>";
echo "<td style=\"text-align:center;\"><input type=\"image\" src=\"/images/deleteX.gif\" border=\"0\" alt=\"Delete\" id='" . $iteml->ID . "' name=\"deleteItem[]\" value=\"" . $item->ID . "\" ></td>";
echo "</tr>";
}
?>
</table>
</form>
I have a dynamic list of items pulled from a database, where the list contains 0 to N items. Each item is listed in a row in an HTML table with an submit button. I need to know which of these buttons, specifically, is clicked on $_POST and to obtain the value of that item.
My present approach is to use an array. However, the $_POST value returns all items in the array, not only the item that was clicked. What's more, the array values do not correlate to the values pulled from the database. The same image submit button may be clicked repeatedly and produce different values.
<?php
else if (isset($_POST["deleteItem"]))
{
foreach ($_POST["deleteItem"] as $value) :
WishList::Delete($value);
endforeach;
}
?>
<form method="post" action="">
<table id="WishListTable">
<?php
$wishlist = WishList::GetAllByID($userID);
foreach ($wishlist as $item)
{
echo "<tr><td>" . $item->Description . "</td>";
echo "<td>";
if ($item->InStock)
{
$primaryEmailAlreadyUsed = true;
echo "In Stock";
}
else {
echo "Out of Stock";
}
echo "</td>";
echo "<td style=\"text-align:center;\"><input type=\"image\" src=\"/images/deleteX.gif\" border=\"0\" alt=\"Delete\" id='" . $iteml->ID . "' name=\"deleteItem[]\" value=\"" . $item->ID . "\" ></td>";
echo "</tr>";
}
?>
</table>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试更改
为“
提交时,deleteItem 的唯一成员将是deleteItem[ID]”
Try changing
to
On submit, the sole member of deleteItem would be deleteItem[ID]
我认为您输入的名称应该只是“deleteItem”而不是“deleteItem[]”。尝试一下。这样您就可以在 $_POST[ 'deleteItem' ] 中获取其 id 的值。
i think that the name of your input should be just "deleteItem" instead of "deleteItem[]". Try it. This way you'll get the value of its id in $_POST[ 'deleteItem' ].
这是对您的具体问题的更概括的答案:
如果我有一个像这样的表单:
在
action.php
中我有:然后,如果我单击
submit1
按钮,输出在action.php
中将是:如果我单击
submit2
按钮,输出将是:我不会将所有提交图像放在数组中,而是生成一个唯一的通过组合某种已知的 ID 为每个对象命名(首选)或通过向它们附加一个计数器变量。然后,您可以执行简单的
isset($_POST['{button name}'])
检查,例如:Here's a more generalized answer to your specific question:
If I have a form like so:
And in
action.php
I have:Then if I click the
submit1
button, the output inaction.php
will be:If I click the
submit2
button, the output will be:Instead of having all of the submit images in an array, I would generate a unique name for each of them, either by combining some sort of known ID (preferable) or by appending a counter variable to them. Then, you can just do a simple
isset($_POST['{button name}'])
check, e.g.: