gpt4 book ai didi

php - 从 PHP 的输入中获取特定的数组值

转载 作者:行者123 更新时间:2023-11-29 07:50:56 24 4
gpt4 key购买 nike

好的,我有一个从 mysql 表中提取的 PHP 数组。该数组是根据表中频繁添加和删除项目的项目生成的。项目名称旁边有一个按钮“提交”。我希望该按钮能够识别同一索引中的项目。然后它将把提交的项目传递到一个新表。

<form class="omb_loginForm" action="inc/contribute_item.php" autocomplete="off" method="POST">
<?php
$item_array;
$index = 0;
$index_2 = 1;
$r = "r";
$b="b";
foreach ($item_array as $id_array){ ?>
<tr id="<?php echo $r.$index_2; ?>">
<td><?php echo $item_array[$index] ?></td>
<td> <?php echo $quantity_array[$index]; ?></td>
<td> <?php echo $price_array[$index];
$selectedItem = $item_array[$index]; ?>
<input type='hidden' name='hidden' value='<?php $selectedItem ?>'>
<input type='submit' name='submit' value"submit">
</form> </td>
<?php $index++;
$index_2++; ?>
</tr>

这是 PHP:

if ($_POST['submit']) {
$connect = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$user_contrib = $_SESSION['first_name'];
$selected = $selectedItem;
$connect->query("UPDATE `items` SET `purchased_by` = '$user_contrib' WHERE `name` = '$selected'");

}

最佳答案

您的方向是正确的,只需确保开始和结束 html 标记正确对齐即可。

每行使用一个表单

如果您想通过隐藏输入传输所选值,请确保每个输入都位于其自己的表单内以及相应的提交按钮:

<!-- row 1: -->
<form action="inc/contribute_item.php" method="post">
<input type="hidden" name="myValue" value="1"/>
<input type="submit" value="submit"/>
</form>

<!-- row 2: -->
<form action="inc/contribute_item.php">
<input type="hidden" name="myValue" value="2"/>
<input type="submit" value="submit"/>
</form>

然后在 PHP 中使用 $_POST['myValue'] 访问所选值.

不要嵌套表单标签。并且不要将表单标签放在 table、tr、td 标签之间。按照打开它们的顺序关闭它们。

更具体地说,您的循环如下所示:

<!-- don't start your form here -->
<table>
<?php foreach(...) { ?>
<tr>
<td>...</td>
<td>
<form action="inc/contribute_item.php" method="post">
<input type="hidden" name="myValue" value="<?= $index ?>"/>
<input type="submit" value="submit"/>
</form>
</td>
</tr>
<?php } ?>
</table>

使用单选按钮或复选框

另一种选择是使用 <input type="radio" ... />每行中的元素。这样您就可以只使用一个全局表单:

<form action="inc/contribute_item.php" method="post">

<table>
<?php foreach(...) { ?>
<tr>
<td>...</td>
<td>
<input type="radio" name="myValue" value="<?= $index ?>"/>
</td>
</tr>
<?php } ?>
</table>

<input type="submit" value="submit"/>
</form>

关于php - 从 PHP 的输入中获取特定的数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26442104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com