Forum Moderators: coopster

Message Too Old, No Replies

Dynamic Radio button check

Need Dynamic Radio Button Check

         

kapiljain

7:18 pm on Feb 21, 2006 (gmt 0)

10+ Year Member



Dear All,

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>&nbsp;</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>&nbsp;</td>
<td ><div align="center" ><? if ($rs["image_path"]!="")
{
$image=$rs["image_path"];
echo "<img src=".$image." class='icon'></img>";
}
?></div></td>

jatar_k

8:41 pm on Feb 21, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld kapiljain,

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

Birdman

10:13 pm on Feb 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jatar_k is correct, which is not quite unusual ;)

Just in case you are using XHTML, you should:

echo ' checked="checked"';

No biggie though. It would still work exactly the same, just give an error if you were to validate for XHTML.

jatar_k

10:24 pm on Feb 21, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



point to Birdman ;)

kapiljain

4:53 am on Feb 22, 2006 (gmt 0)

10+ Year Member



Thanks a lot dear administrator & Birdman for helping me instantly. I cannot hope for such polite and instant reply in forums thanks a lot for lovely forum now always i will disturb :) to solve my problems and hope you'll help me. Thanks a lot

I am trying this code and hope this will run definitly
- Kapil

kapiljain

5:13 am on Feb 22, 2006 (gmt 0)

10+ Year Member



Yes it works thanks a lot ....

jatar_k

6:09 am on Feb 22, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



glad to help