Ravi Ravi - 5 months ago 17
Javascript Question

I am not able to check Multiple radio button

Its Question and Four Options and check any one. Problem When Checked button to Next Question then previous check was unchecked.

Code as bellow:

<form method="post" id="submitme">
<table class="ques_list" border=1 style="width:100%">
<?php
$result=mysql_query("SELECT * FROM questions ORDER BY RAND() LIMIT 20");
while ($row = mysql_fetch_array($result)) { ?>
<tr>
<td colspan="2"><?php echo $row['ques_title']."<br />"; ?></td>
</tr>
<tr>
<td><input type="radio" name="opt_value" value="1" /><?php echo $row['ques_opt_1']."<br />"; ?></td>
<td><input type="radio" name="opt_value" value="2" /><?php echo $row['ques_opt_2']."<br />"; ?></td>
</tr>
<tr>
<td><input type="radio" name="opt_value" value="3" /><?php echo $row['ques_opt_3']."<br />"; ?></td>
<td><input type="radio" name="opt_value" value="4" /><?php echo $row['ques_opt_4']."<br />"; ?></td>
</tr>
<?php } ?>
<tr><td colspan="2"><button type="submit" name="btn-submit" class="btn-submit">Submit</button></td></tr>
</table>
</form>


And How can i get his value ?
Suggest me.
Thanks.

Answer

You can add an index to the names of the radio buttons.

<form method="post" id="submitme">
    <table class="ques_list" border=1 style="width:100%">
    <?php 
        $result=mysql_query("SELECT * FROM questions ORDER BY RAND() LIMIT 20");
        while ($row = mysql_fetch_array($result)) { ?>
            <tr>
                <td colspan="2"><?php echo $row['ques_title']."<br />"; ?></td>
            </tr>
            <tr>
                <td><input type="radio" name="opt_value_<?php echo $row['ques_id']; ?>" value="1" /><?php echo $row['ques_opt_1']."<br />"; ?></td>
                <td><input type="radio" name="opt_value_<?php echo $row['ques_id']; ?>" value="2" /><?php echo $row['ques_opt_2']."<br />"; ?></td>
            </tr>
            <tr>
                <td><input type="radio" name="opt_value_<?php echo $row['ques_id']; ?>" value="3" /><?php echo $row['ques_opt_3']."<br />"; ?></td>
                <td><input type="radio" name="opt_value_<?php echo $row['ques_id']; ?>" value="4" /><?php echo $row['ques_opt_4']."<br />"; ?></td>
            </tr>
        <?php } ?>
        <tr><td colspan="2"><button type="submit" name="btn-submit" class="btn-submit">Submit</button></td></tr>
    </table>
</form> 

Edit:

Then you can read your answers as follows:
($row['ques_id']has to be unique for each question and the answers should be the only posted values)

if( $_SERVER['REQUEST_METHOD'] === 'post'){
   foreach( $_POST as $question => $answer ){
       $quest_id = preg_replace( '/[^0-9]/' , '' , $question );

       //make database query to check answer
       //    $quest_id is the question ID 
       //    $answer is number of the selected answer
   }
}
Comments