1

For a survey app which has a combination of radiobuttons and checkboxes. How do I use PHP to insert the selected values to mysql db using php? This is the query for fetching the questions from db:

while($row2 = $result2->fetch_assoc())
{

if($row2["subtype"] =="radio")
{
 echo "<input id = \"radio\" class='radio_input' type=\"radio\" name=answergroup[".$row["PK_QUESTION_ID"].
"] value=".$row2["opt_id"].">".$row2["opt_text"]."</input>";

else if($row2["subtype"] =="checkbox")
{
echo "<input id = \"checkbox\" class='radio_input' type=\"checkbox\" name=answergroup[".$row["PK_QUESTION_ID"].
"] value=".$row2["opt_id"].">".$row2["opt_text"]."</input>";

}
}

I tried using $_POST['answergroup'] for fetching the selected options, but in the case of a checkbox, only one option is being fetched even if there are multiple selections made.

1
  • What you getting in print_r($_POST) ? Commented Apr 28, 2017 at 9:02

1 Answer 1

1

When you are dealing with multiple checkboxes with the same name, in that case the name of checkboxes must be an array like:

<input type="checkbox" name="options[]" value="1">

On form submit you can get the values like:

$options = $_POST['options'];

here $options is an array, so use foreach() to get its elements.

Sign up to request clarification or add additional context in comments.

1 Comment

The name is an array. The issue I'm facing is when there's a combination of radio buttons and checkboxes...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.