By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,965 Members | 1,702 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,965 IT Pros & Developers. It's quick & easy.

PHP form - issues with radio buttons

P: 14
I am trying to create a form to be used for testing during a tutorial (at the end of each module the user will be given a test).

It contains a number of questions, each with a question and 4 possible answers. The answers are radio buttons, so the user can only select 1 for each question.

I am currently selecting 5 questions randomly from a database and displaying them using a loop:

$a = 1;
while ($row = mysql_fetch_assoc($result)){ //$result is the question data from the db
<TABLE>
<TR><TD><b><? echo "$a. ", $row ['Question']. "<br/>" ?></b></TD></TR>
<TR><TD><input type="radio" name="Question$a" value="1"> <? print $row ['Opt_1']. "<br/>" ?></TD></TR>
<TR><TD><input type="radio" name="Question$a" value="2"> <? print $row ['Opt_2']. "<br/>" ?></TD></TR>
<TR><TD><input type="radio" name="Question$a" value="3"> <? print $row ['Opt_3']. "<br/>" ?></TD></TR>
<TR><TD><input type="radio" name="Question$a" value="4"> <? print $row ['Opt_4']. "<br/>" ?></TD></TR>
</TABLE>
<?php
print "<br/>";
$a++;
}//end while

I have used name="Question$a" so that each answer set will have a different name (i.e. Question1, 2,...) so i can then use $answer1 = $_POST ['Question1'] later to get the user's answer for each question. This isn't working though.

Firstly, it only allows me to select one radio button on the whole page (rather than one per question) and it is not giving each set a different name (the name="Question$a" bit).

I have tried other options but am really struggling now. Any help will be much appreciated.
Apr 22 '07 #1
Share this Question
Share on Google+
4 Replies


ak1dnar
Expert 100+
P: 1,584
Try to understand what i have done here. and replace your $result String for the while loop.

[PHP]<?php
$a = 1;
while ($a<=5)
{
?>
<TABLE>
<TR><TD><b><? echo "$a. ", $row ['Question']. "<br/>" ?></b></TD></TR>
<TR><TD><input type="radio" name="<?php print 'Question'.$a ?>" value="1"> <? print $row ['Opt_1']. "<br/>" ?></TD></TR>
<TR><TD><input type="radio" name="<?php print 'Question'.$a ?>" value="2"> <? print $row ['Opt_2']. "<br/>" ?></TD></TR>
<TR><TD><input type="radio" name="<?php print 'Question'.$a ?>" value="3"> <? print $row ['Opt_3']. "<br/>" ?></TD></TR>
<TR><TD><input type="radio" name="<?php print 'Question'.$a ?>" value="4"> <? print $row ['Opt_4']. "<br/>" ?></TD></TR>
</TABLE>
<?php
print "<br/>";
$a++;
}
?> [/PHP]
Apr 23 '07 #2

P: 14
Thanks for the help, I've used that code and it solves the two issues. However, it outputs the same question for each (i.e. it isn't cycling through the query result which is an array containing the 5 questions and options). How do i get it to do this?

thanks again.
Apr 23 '07 #3

P: 14
No worries, got it to work. I used my original code with the change you made to the name="" section of the radio buttons. So it now has name="<?php print 'Question'.$a ?>" instead.

It seems to work fine, but if you see any issues with this let me know.

Thanks for the help.
Apr 23 '07 #4

ak1dnar
Expert 100+
P: 1,584
[PHP]<?php
include 'dbcon.php';
$sql = 'select * from products LIMIT 5';// You have to create a SQL Query as per your Reqirement
$result = mysql_query($sql);
$a = 1;
while ($row = mysql_fetch_assoc($result))
{
?>
<TABLE>
<TR><TD><b><? echo "$a. ", $row['p_id']. "<br/>" ?></b></TD></TR>
<TR><TD><input type="radio" name="<?php print 'Question'.$a ?>" value="1"> <? print $row ['Opt_1']. "<br/>" ?></TD></TR>
<TR><TD><input type="radio" name="<?php print 'Question'.$a ?>" value="2"> <? print $row ['Opt_2']. "<br/>" ?></TD></TR>
<TR><TD><input type="radio" name="<?php print 'Question'.$a ?>" value="3"> <? print $row ['Opt_3']. "<br/>" ?></TD></TR>
<TR><TD><input type="radio" name="<?php print 'Question'.$a ?>" value="4"> <? print $row ['Opt_4']. "<br/>" ?></TD></TR>
</TABLE>
<?php
print "<br/>";
$a++;
}
?> [/PHP]
Apr 23 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.