输入类型数组的 $_POST 值 =“image”大批?

发布于 2024-11-08 00:03:50 字数 1120 浏览 0 评论 0原文

我有一个从数据库中提取的动态项目列表,其中该列表包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅听莫相离 2024-11-15 00:03:50

尝试更改

name=\"deleteItem[]\"

为“

name=\"deleteItem[" . $item->ID . "]\"

提交时,deleteItem 的唯一成员将是deleteItem[ID]”

list($deletedItemID) = array_keys($_POST['deleteItem']);

Try changing

name=\"deleteItem[]\"

to

name=\"deleteItem[" . $item->ID . "]\"

On submit, the sole member of deleteItem would be deleteItem[ID]

list($deletedItemID) = array_keys($_POST['deleteItem']);
南城追梦 2024-11-15 00:03:50

我认为您输入的名称应该只是“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' ].

旧夏天 2024-11-15 00:03:50

这是对您的具体问题的更概括的答案:

如果我有一个像这样的表单:

<form method = 'post' action = 'action.php'>
    <input type = 'submit' name = 'submit1' value = 'Submit 1'/>
    <input type = 'submit' name = 'submit2' value = 'Submit 2'/>
</form>

action.php 中我有:

<?php
    foreach($_POST as $k => $v)
        echo($k . ": " . $v . "<br/>")
?>

然后,如果我单击 submit1 按钮,输出在 action.php 中将是:

submit1: Submit 1

如果我单击 submit2 按钮,输出将是:

submit2: Submit 2

我不会将所有提交图像放在数组中,而是生成一个唯一的通过组合某种已知的 ID 为每个对象命名(首选)或通过向它们附加一个计数器变量。然后,您可以执行简单的 isset($_POST['{button name}']) 检查,例如:

<input type = 'image' src = '...' name = 'deleteItem<?php echo($iteml->ID); ?>'/>

Here's a more generalized answer to your specific question:

If I have a form like so:

<form method = 'post' action = 'action.php'>
    <input type = 'submit' name = 'submit1' value = 'Submit 1'/>
    <input type = 'submit' name = 'submit2' value = 'Submit 2'/>
</form>

And in action.php I have:

<?php
    foreach($_POST as $k => $v)
        echo($k . ": " . $v . "<br/>")
?>

Then if I click the submit1 button, the output in action.php will be:

submit1: Submit 1

If I click the submit2 button, the output will be:

submit2: Submit 2

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

<input type = 'image' src = '...' name = 'deleteItem<?php echo($iteml->ID); ?>'/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文