多个输入字段的单选按钮发布数据 [英] Radio button post data of multiple input fields

查看:48
本文介绍了多个输入字段的单选按钮发布数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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 [] 来处理与单选按钮相同的数量,但这将返回所有值.

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.

我已经搜索了很多,然后用谷歌搜索了一堆,但是还没有找到合适的答案,但是在我看来,这已经被大量使用了.请让我知道我在这里想念的东西.

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!

这篇关于多个输入字段的单选按钮发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆