Radio button post data of multiple input fields(单选按钮发布多个输入字段的数据)
问题描述
我的 PHP 页面中有一个表单,它是通过数组循环创建的.
I have a form in my PHP page which is created by a loop through an array.
echo '<form method="POST" name="add_to_cart_form">
<div id="Product_add_sizes">
<table border="0" cellpadding="2" class="product_txt_font" align="center">
<input type="hidden" name="product_id" value="'.$arr_get_products[$c]['id'].'">';
for ($d = 0; $d < count($arr_get_product_details); $d++)
{
echo '<tr>
<td>
<input type="radio" name="size[]" value="'.$arr_get_product_details[$d]['size'].'">
</td>
<td>
<input class="qty" type="text" size="3" name="amount" value="1">
</td>
</tr>';
}
echo '</table>
<input type="submit" name="add_to_chart" value="Add Product" />
</div>
</form>';
现在,当我发布此表单时,我正在寻找一种方法来获取属于所选单选按钮的数量的输入.我只需要所选单选按钮行中的数据.不需要其他数据,因为我想将此产品添加到购物车数组中.
Now when I post this form, I'm searching for a way to get the input of qty which belongs to the selected radio button. I only need the data from the selected radio button row. The other data is not needed as I want to add this product to a shopping cart array.
if (isset($_POST['add_to_chart']))
{
$product_id = $_POST['product_id'];
$size = $_POST['size'][0];
$qty = $_POST['amount'];
}
发布页面时,我知道 size[]
需要什么尺寸数组,但我无法获得与所选单选按钮匹配的相关数量值.
When posting the page, I know what size is wanted cause of the size[]
array but I can't get the related qty value that matches the selected radio button.
我试图通过将 qty 设为数组 qty[]
来以与单选按钮相同的方式处理 qty,但这将返回所有值.
I've tried to treat the qty the same way as the radio button by making it an array qty[]
but that will return me all values.
我搜索了 SO 并用谷歌搜索了一堆,但没有找到一个像样的答案,但在我看来,这已经被大量使用了.请让我知道我在这里缺少什么.
I've search SO and googled a bunch but haven't found a decent answer, but It looks to me that this is used a lot. Please let me know what i'm missing here.
非常感谢任何帮助!
推荐答案
我通过以下方式解决了这个问题.
I solved the issue by doing the following.
上面的人都让我走上了正轨,没有他们就不可能做到!
The guys above here all got me on the right track and couldn't have done it without them!
改变了
<input type="radio" name="size[]" value="'.$arr_get_product_details[$d]['size'].'">
<input class="qty" type="text" size="3" name="amount" value="1">
到
<input type="radio" name="size['.$d.']" value="'.$arr_get_product_details[$d]['size'].'">
<input class="qty" type="text" size="3" name="amount['.$d.']" value="1">
也改了
if (isset($_POST['add_to_chart']))
{
$product_id = $_POST['product_id'];
$size = $_POST['size'][0];
$qty = $_POST['amount'];
}
到这里
if (isset($_POST['add_to_chart']))
{
// Array ID key
$key = key($_POST['size']);
$product_id = $_POST['product_id'];
$size = $_POST['size'][$key];
$qty = $_POST['amount'][$key];
}
像魅力一样工作!感谢大家提出非常有帮助的意见!
Works like a charm! Thank you all for your very helpful comments!
这篇关于单选按钮发布多个输入字段的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:单选按钮发布多个输入字段的数据
基础教程推荐
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01