php - How to get PHP to match textbox with corresponding checkbox from a form
问题描述
Basically, I have about 700 customers in a database and I show them in a page with a loop, which goes through of all 700 customers. This loop adds a checkbox next to each customer, so you can select which one of them you want to go onto the next days schedule, on average this will be about 30 customers.
This is my form:
<table>
<tr>
<td class="search_result_fullname"><?php echo $row['fullname']; ?></td>
<td class="search_result_price">£<input type="text" name="price[]" value="<?php echo $price; ?>" class="schedule_price"></td>
<td class="search_result_phone"><a href="tel:<?php echo $phone; ?>"><?php echo $phone; ?></a></td>
<td class="search_result_checkbox"><input type="checkbox" name="customerid[]" value="<?php echo $row['id']; ?>"></td>
</tr>
<tr>
<td colspan="5"><input type="text" name="remarks[]" placeholder="Remarks" class="remarks"></td>
</tr>
</table>
Once this form is submitted, then it reloads the page and I have a if(isset($_POST['add'])){
in place. I've tried various different loops and I cant for the life of me work out how to do it.
So, the form is submitting an array of only checkboxs that are checked. If I call print_r($_POST['customerid']);
to verify it shows that this is working as expected. Then, if i select customer 50
and 51
, then it should return an array with just those two.
Now the problem is that I also want to get the correct price value for each customer (50
and 51
), but the array for $_POST['price']
is submitting the price for all 700 customers. How do I get the price to correspond to the correct customers that I've checked?
Any support is greatly appreciated.
解决方案
您可以将price
数组设置为按客户 ID 进行索引:
<input type="text" name="price[<?=$row['id']?>]" value="<?=$price?>" class="schedule_price">
...然后按客户 ID 参考价格:
$customerIds = $_POST['customerid'];
$prices = $_POST['price'];
foreach ($customerIds as $customerId) {
$price = $prices[$customerId];
}
推荐阅读
- bash - 从 bash 变量中删除前导换行符
- f# - 使用 F# 向 ASP.Net Core 3.0 添加授权策略
- javascript - 类事件绑定和解除绑定
- django - 让 django 为 html 选择标签选择正确的选项
- snowflake-cloud-data-platform - 雪花 - 如何引用名称中带有特殊字符的指定表的内部阶段?
- c# - 如何在 Xamarin 中显示带有三个点的工具栏?
- swagger - OpenApi:从多个文件中生成 java/kotlin DTO
- reactjs - 类型断言对象字面量打字稿
- android - 从 Mac 到 Android 的 ADB 连接被拒绝
- perl - 我有两个使用 Perl 转换数据的问题?