Multiple radio button array for php form
When you're unsure of how to handle the HTML markup that you've set up, you should var_dump($_POST)
the values that are sent to the PHP handler page so you know what the format will look like, that way you can proceed from there.
When I created your HTML and tested it with a var_dump
and some random selections, the output was
array(2) { ["answer"]=> array(3) { [1]=> string(1) "5" [2]=> string(1) "3" [3]=> string(1) "4" } ["submit"]=> string(6) "Submit" }
Notice that there is an array within the $_POST['answer']
variable. So you should foreach
over each element in that array to handle each respective value:
foreach ($_POST['answer'] as $answer) {
// do stuff with the answer
}
If you need to work with the answer number that you defined in the POST
array, you can foreach
with a key:
foreach ($_POST['answer'] as $answerNum => $answer) {
// do stuff with $answerNum and $answer
}
You can, of course, access your answer by its number directly:
if (!empty($_POST['answer'][1])) { // To ensure that the value is being sent
// do stuff with $_POST['answer'][1]
}
i have multiple radio button in same name as array and same value
I am pretty sure that radio buttons don't get sent to the server if none of the values is selected. In your html you really have 4 different radio button groups so only the ones that have a selected value, get sent.
If you want 4 groups where a value always gets sent for each group, you should do something like this:
<input type="radio" name="radio_name[1]" value="1" id="radio1" checked>
<input type="radio" name="radio_name[1]" value="0">
<label for="radio1">Set as Default</label>
<input type="radio" name="radio_name[2]" value="1" id="radio2">
<input type="radio" name="radio_name[2]" value="0" checked>
<label for="radio2">Set as Default</label>
// etc.
This way you will have independent radio button groups and you will get the result you want.
how-to submit form with multiple groups of radio buttons?
You just need to remove the other two submit buttons and only display one. As long as it is inside the <form>
it will submit all the radio button "groups" to the $_POST
.
Also, remove the select tags as you are using them incorrectly. Only an option or optgroup should be nested inside a select element.
You will need to reference each one individually to see the selected response:
echo $_POST['radioA'];
echo $_POST['radioB'];
echo $_POST['radioC'];
Can multiple groups of HTML radio buttons be made to return a PHP array after a submit?
If you need to submit an array of values, all you need to do is use the square bracket annotation inside the name attribute: name="someName[]"
and PHP will detect it as an array when submitted.
Now if you organize your elements in this fashion:
<form method="POST">
<input type="radio" name="id[100]" value="Restore">
<input type="radio" name="id[100]" value="Keep">
<input type="radio" name="id[100]" value="Delete">
<input type="radio" name="id[101]" value="Restore">
<input type="radio" name="id[101]" value="Keep">
<input type="radio" name="id[101]" value="Delete">
<input type="radio" name="id[999]" value="Restore">
<input type="radio" name="id[999]" value="Keep">
<input type="radio" name="id[999]" value="Delete">
<input type="submit" name="send" value="Send">
</form>
and submit the form, the $_POST
variable will capture the values like this:
array(
'id' => array(
100 => 'Restore',
101 => 'Keep',
999 => 'Delete',
),
'send' => 'Send',
)
How to Get the multiple radio buttons values in PHP which having names dynamically
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 2POST['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>";
?>
Related Topics
How to Join Three Tables in Codeigniter
Sort Array by Value Alphabetically PHP
Convert PHP Array to JSON Tree
PHP 7 and Strict "Resource" Types
Add Prefix to Auto-Increment in MySQL Db
Cookies Not Working on Different Pages
Reverse Order of String Like "Hello Word" Reverse as "Word Hello" in PHP
How to Get an Option Previously Set with Curl_Setopt()
How to Add a PHP Simplexmlelement to Another Simplexmlelement
Php: What Is the Complexity [I.E O(1),O(N)] of the Function 'Count'
Strtolower() for Unicode/Multibyte Strings
Make Text Between Asterisks Bold
How to Make PHP Curl Request with Port Number
How to Solve "Non-Static Method Xxx:Xxx() Should Not Be Called Statically in PHP 5.4