Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

forms

php

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.

<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>
like image 231
Kumaran Avatar asked Dec 01 '15 05:12

Kumaran


People also ask

Can we select multiple radio buttons?

Only one radio button in a group can be selected at the same time. Note: The radio group must have share the same name (the value of the name attribute) to be treated as a group.

Can multiple radio buttons have same name?

Radio buttons allow only one choice within a group of buttons. Each radio button within a group should have the same name. You can create more than one group of radio buttons by using different names.

How do I get the value of a radio button in PHP?

To get selected value of a radio button: php if (isset($_POST['submit'])) { if(isset($_POST['radio'])) { echo "You have selected :". $_POST['radio']; // Displaying Selected Value } ?>


1 Answers

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>";
?>
like image 129
Suyog Avatar answered Oct 28 '22 23:10

Suyog