0

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/>";
}
2
Contributors
10
Replies
38
Views
4 Years
Discussion Span
Last Post by ferdinandmucos
Featured Replies
  • google: [http://www.echoecho.com/htmlforms09.htm](http://www.echoecho.com/htmlforms09.htm) [http://www.html-form-guide.com/php-form/php-form-checkbox.html](http://www.html-form-guide.com/php-form/php-form-checkbox.html) [http://stackoverflow.com/questions/2268887/php-checkbox-input](http://stackoverflow.com/questions/2268887/php-checkbox-input) Read More

  • 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) Read More

  • 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++; … Read More

0

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/>";
}
0

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.

1

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)

Edited by pzuurveen

0

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

Edited by ferdinandmucos

1

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++;
    }
0

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.

Edited by ferdinandmucos

0

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);
}

Edited by ferdinandmucos

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.