Forum Moderators: coopster
I need urgent help for following :
What i need is, i want to check radio button dynamically according to mysql value.
Explanation :
I have online quiz system in which user can mark any question with 4-6 option for review in the last of exam. So requirement is that when somebody go to review questions so he'll get his question with radio button checked for option he selected at the time of quiz attempt. My all value comes from mysql database and generates Review as per user selection database values consist question, id, user_choice value such as 1,2,3,4 (option what he selected in the begining) and my option radio buttons is group with one name which dynamically received from db i.e (question_id).
So just wanted that i can retain user selection in radio button from mysql database
code as follows to generate radio button:
<td ><input name="review" type="checkbox" id="review" value="checkbox" /></td>
<td height="25" class="review" >Mark it for Review </td>
</tr>
<tr>
<td width="40" class="question">Q.<? print($rs["series"]);?> </td>
<td height="25" class="question" > <? print($rs["question"]);?></td>
</tr>
<tr>
<td><span class="rowtwo">
<input name="quest_id" type="hidden" id="quest_id2"
value="<? print($rs["quesid"]);?>" size="7"/>
<input name="user_question" type="hidden" id="user_question2"
value="<? print($rs["series"]);?>" size="7"/>
</span></td>
<td ><span class="rowtwo">
<input name="quesopt" type="hidden" id="quesopt2"
value="<? print($quesopt);?>" size="7"/>
</span></td>
</tr>
<tr>
<td> </td>
<td><p>
<label>
<input type="radio" name="<? print($rs["quesid"]);?>" value="1" />
<? print($rs["opt1"]);?></label>
<br />
<label>
<input type="radio" name="<? print($rs["quesid"]);?>" value="2" />
<? print($rs["opt2"]);?></label>
<br />
<label>
<input type="radio" name="<? print($rs["quesid"]);?>" value="3" />
<? print($rs["opt3"]);?></label>
<br />
<label>
<input type="radio" name="<? print($rs["quesid"]);?>" value="4" />
<? print($rs["opt4"]);?></label>
<br />
<?
if ($rs["opt5"]!="")
{
?>
<label>
<input type="radio" name="<? print($rs["quesid"]);?>" value="5" />
<? print($rs["opt5"]);?></label>
<br />
<?
}
?>
<?
if ($rs["opt6"]!="")
{
?>
<label>
<input type="radio" name="<? print($rs["quesid"]);?>" value="6" />
<? print($rs["opt6"]);?></label>
<br />
<? }?>
</p></td>
</tr>
<tr>
<td> </td>
<td ><div align="center" ><? if ($rs["image_path"]!="")
{
$image=$rs["image_path"];
echo "<img src=".$image." class='icon'></img>";
}
?></div></td>
inside the tag for the radio input element you need to compare with what the user selected and if it matches echo 'checked'.
I am guessing $rs is where the value is and I am also guessing it is called user_choice. Since this is a number then I am also guessing you could compare it to the option number something like this.
<input type="radio" name="<? print($rs["quesid"]);?>" value="1" <? if($rs['user_choice'] == 1) echo ' checked';?>/>
I think that's right
I am trying this code and hope this will run definitly
- Kapil