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 中的逻辑/样式 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- Laravel 仓库 2022-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01