Radio button post data of multiple input fields

前端 未结 4 864
半阙折子戏
半阙折子戏 2021-01-28 00:18

I have a form in my PHP page which is created by a loop through an array.

echo \'
相关标签:
4条回答
  • 2021-01-28 00:42

    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!

    Changed

    <input type="radio" name="size[]" value="'.$arr_get_product_details[$d]['size'].'">
    <input class="qty" type="text" size="3" name="amount" value="1">
    

    To

    <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">
    

    Also changed

    if (isset($_POST['add_to_chart']))
    {
        $product_id = $_POST['product_id'];
        $size = $_POST['size'][0];
        $qty = $_POST['amount'];
    }
    

    To this

    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!

    0 讨论(0)
  • 2021-01-28 00:46

    Radio buttons should all use the same name. Only the selected radio button value is submitted:

    <input type="radio" name="Radiosize" value="'.$arr_get_product_details[$d]['size'].'">
    

    then:

    $size = $_POST['Radiosize'];
    

    There is no need to look at it as an array - it isn't submitted as one. Radio Buttons are not like checkboxes in form processing.

    0 讨论(0)
  • 2021-01-28 01:00

    Form code:

        <input type="radio" name="size" value="'.$arr_get_product_details[$d]['size'].'">
        <input class="qty" type="text" size="3" name="amount['.$arr_get_product_details[$d]['size'].']" value="1">
    

    And then you will have value of size. And array of amounts with size keys.

        $size   = $_POST['size'];
        $amount = $_POST['amount'][$size];
    
    0 讨论(0)
  • 2021-01-28 01:02

    change

    <input class="qty" type="text" size="3" name="amount" value="1">
    

    to

    <input class="qty" type="text" size="3" name="amount[]" value="1">
    

    and then you will have 2 arrays that will have same size.

    <?php
        $size = $_POST['size'][0];
        $amount = $_POST['amount'][$size];
    ?>
    
    0 讨论(0)
提交回复
热议问题