How to Get the multiple radio buttons values in PHP which having names dynamically

前端 未结 4 2232
不思量自难忘°
不思量自难忘° 2021-02-09 06:17

I am having following codings in my form..how do I get the value of all radio button values on submit which is inside looping? Or give me any other solution for this.



        
相关标签:
4条回答
  • 2021-02-09 06:56

    Yes, as Sean commented, try this:

    <form action="res.php" method="post">
    <?php
        for($i=1;$i<=5;$i++)
        {
    ?>
            <div class="well well-sm well-primary">
                <input type="hidden" name="ques"/>Questions?
            </div>
            <div class="well well-sm">
                <div class="radio">
                    <label>
                        <input type="radio" name="optradio[<?php echo $i; ?>]" value="a">Option 1
                    </label>
                </div>
                <div class="radio">
                    <label>
                        <input type="radio" name="optradio[<?php echo $i; ?>]" value="b">Option 2
                    </label>
                </div>
                <div class="radio">
                    <label>
                        <input type="radio" name="optradio[<?php echo $i; ?>]" value="c">Option 3
                    </label>
                </div>
            </div>
        <?php
        }
        ?>
        <button type="submit" class="btn btn-success" name="submit">Finish</button>
    </form>
    

    and then use the below in PHP side to get radio button value as :

    foreach ($_POST['optradio'] as $optNum => $option) {
        // do stuff with $optNum and $option
    }
    
    0 讨论(0)
  • 2021-02-09 07:02

    Use array of radio button as follows

    <form method="post">
        <?php
        for($i=1;$i<=5;$i++)
        {
            ?>
            <div class="well well-sm well-primary">
                <input type="hidden" name="ques"/>Questions?
            </div>
            <div class="well well-sm">
                <div class="radio">
                    <label>
                    <input type="radio" name="optradio[<?php echo $i; ?>]" value="a">Option 1</label>
                </div>
                <div class="radio">
                    <label>
                    <input type="radio" name="optradio[<?php echo $i; ?>]" value="b">Option 2</label>
                </div>
                <div class="radio">
                    <label>
                    <input type="radio" name="optradio[<?php echo $i; ?>]" value="c">Option 3</label>
                </div>
            </div>
            <?php
        }
        ?>
        <button type="submit" class="btn btn-success" name="submit">Finish</button>
    </form>
    

    To access the posted values, you can simply use $_POST['optradio']

    Considering the selection for 5 questions to be Option 1, Option 2, Option 3, Option 1, Option 2 POST['optradio'] will give array like

    Array ( [1] => a [2] => b [3] => c [4] => a [5] => b )

    To access sigle values from this array, you can use foreach loop as,

    <?php 
     foreach($_POST['optradio'] as $option_num => $option_val)
        echo $option_num." ".$option_val."<br>";
    ?>
    
    0 讨论(0)
  • 2021-02-09 07:03

    Try this:
    <input type="radio" name="optradio[]" value="a">

    And in PHP file, $_POST['optradio'] will result as an array.

    0 讨论(0)
  • 2021-02-09 07:21

    take a one hidden input for storing radio button name array in for loop like

    <input type="hidden" name="testradio[]" value="optradio<?php echo $i; ?>">
    

    and then fetch radio button value using foreach

    $rdobtn = $_POST['testradio'];
    $idx = 0;
    
        foreach($rdobtn as $val){
    
          $rdovalue = $val[$idx];
    
          // perform opertation using above $rdovalue variable.
    
           $idx++;
        }
    

    }

    0 讨论(0)
提交回复
热议问题