How do I Change the select option to check boxes in the following code:

foreach($qas as $k=>$v)
{
echo "<input id='text' style='width:40%' type='text' value='".$v['question']."' name='questions[]' >";
echo "<select name='selected_answers[]'>";
foreach($v['answers'] as $answer){
echo "<option value='".$answer."'>".$answer."</option>";
}
echo "</select>";
echo "<br/><br/>";
}

Recommended Answers

You should change the name of radiobutten for each questing
something like:

echo "<input type=\"radio\" name=\"selected_answers".$v['idnr']."\" value='\".$answer.\"' />";

$v['question'] probably has spaces, so don't use that.
do something simular for the id of the input statement, id supposed to be unique. (line 3)

Jump to Post

If you don't have a variable like $v['id'] or $v['idnr'] you cal alway count the questions yourself

$questionNr=0;    
foreach($qas as $k=>$v)
    {
    echo "<input id='question$questionNr' style='width:40%' type='text' 
                value='".$v['question']."' name='questions[]' >";
    echo '<br /><br />';
    foreach($v['answers'] as $answer)
        {
        echo "<input type=\"radio\" name=\"selected_answers$questionNr\" value='\".$answer.\"' />";
        echo "$answer";
        echo "<br/>";
        }
    $questionNr++; …
Jump to Post

All 10 Replies

Do you main like this ?

Thats what I have, Now to change the pull down menu to be check boxes.

This is how I have been trying to go about it but its not functioning:

foreach($qas as $k=>$v)
{
echo "<input id='text' style='width:40%' type='text' value='".$v['question']."' name='questions[]' >";

foreach($v['answers'] as $answer)
{
<input type="radio" name= "selected_answers[]" value='".$answer."' />;
echo "$answer";
}
echo "<br/><br/>";
}

I have managed to put the answers in a checkbox, but the chech boxes assumes that one checkbox, checked is an answers for all the questions.

This is how I have done it:

foreach($qas as $k=>$v)
{
echo "<input id='text' style='width:40%' type='text' value='".$v['question']."' name='questions[]' >";
echo '<br /><br />';
foreach($v['answers'] as $answer)
{
echo "<input type=\"radio\" name=\"selected_answers[]\" value='\".$answer.\"' />";
echo "$answer";
echo "<br/>";
}
}

How can I make the checkboxes each unique to each question? Thank you.

You should change the name of radiobutten for each questing
something like:

echo "<input type=\"radio\" name=\"selected_answers".$v['idnr']."\" value='\".$answer.\"' />";

$v['question'] probably has spaces, so don't use that.
do something simular for the id of the input statement, id supposed to be unique. (line 3)

I seem not to get how to implement that How do i go about it. Thank you.

If you don't have a variable like $v['id'] or $v['idnr'] you cal alway count the questions yourself

$questionNr=0;    
foreach($qas as $k=>$v)
    {
    echo "<input id='question$questionNr' style='width:40%' type='text' 
                value='".$v['question']."' name='questions[]' >";
    echo '<br /><br />';
    foreach($v['answers'] as $answer)
        {
        echo "<input type=\"radio\" name=\"selected_answers$questionNr\" value='\".$answer.\"' />";
        echo "$answer";
        echo "<br/>";
        }
    $questionNr++;
    }

The problem still persists, I select one check box for a particular question, but it still recognises one checkbox as an answer for all the questions. Selecting one deselects the already selected one.

It does function, Thank you Pzuurveen. I hope when I post I can also use:

/Posting of the chosen answers
if(isset($_POST['submit']))
{
$answers = $_POST['selected_answers$questionNr'];
echo '<b><u>THE ANSWERS YOU HAVE CHOSEN ARE:</u></b><br /><br />';
print_r($answers);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.