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.
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
}
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>";
?>
Try this:
<input type="radio" name="optradio[]" value="a">
And in PHP file,
$_POST['optradio']
will result as an array.
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++;
}
}