1

I have two questions with radio button (each question having 4 radio) in PHP

But there is single check in all 8 radio buttons, but it should be, with 4-4 radio buttons.

Look: https://i.stack.imgur.com/3qiXh.jpg

My Code:

<form method="post" enctype="multipart/form-data" class="form-horizontal">

    <?php


    $lstmt = $user->runQuery("SELECT * FROM mcq WHERE LRN=:lrn ORDER BY Sr ASC ");
    $lstmt->bindparam(":lrn",$id);
    $lstmt->execute();

    if($lstmt->rowCount() > 0)
    {
    $i=0;
        while($lrow=$lstmt->fetch(PDO::FETCH_ASSOC))
        {
            extract($lrow);
            $i++;

?>    
    <div>
    <h1><?php echo $i; ?>)&nbsp;<?php echo $Question; ?></h1></br>
    <h2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="radio" value="<?php echo $Oa; ?>">&nbsp;&nbsp;A) <?php echo $Oa; ?></h2>
    <h2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="radio" value="<?php echo $Ob; ?>">&nbsp;&nbsp;B) <?php echo $Ob; ?></h2>
    <h2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="radio" value="<?php echo $Oc; ?>">&nbsp;&nbsp;C) <?php echo $Oc; ?></h2>
    <h2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="radio" value="<?php echo $Od; ?>">&nbsp;&nbsp;D) <?php echo $Od; ?></h2>
    </div>
    <hr></br></br>
    <?php
    }
    }
    ?>
    <button class="btn btn-large btn-primary" type="submit" name="btn-submit">Submit</button>
</form>
<?php
}
if(isset($_POST['btn-submit']))
{
    echo $ufname = trim($_POST['radio']);
}
?>
MKJ
  • 133
  • 1
  • 1
  • 11

1 Answers1

0

Update your radio input name attribute just like this,

<div>
<h1><?php echo $i; ?>)&nbsp;<?php echo $Question; ?></h1></br>
<h2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="radio<?php echo $i; ?>?>" value="<?php echo $Oa; ?>">&nbsp;&nbsp;A) <?php echo $Oa; ?></h2>
<h2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="radio<?php echo $i; ?>" value="<?php echo $Ob; ?>">&nbsp;&nbsp;B) <?php echo $Ob; ?></h2>
<h2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="radio<?php echo $i; ?>" value="<?php echo $Oc; ?>">&nbsp;&nbsp;C) <?php echo $Oc; ?></h2>
<h2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="radio<?php echo $i; ?>" value="<?php echo $Od; ?>">&nbsp;&nbsp;D) <?php echo $Od; ?></h2>
</div>

And for submitting:

<?php
}
$count = $i;
for($j = 1; $j <= $count; $j++){ if(isset($_POST['btn-submit'])) { 
echo $ufname = trim($_POST['radio'.$j]); 
}
}
?>

It will definitely solve your problem,

MKJ
  • 133
  • 1
  • 1
  • 11
Ritesh Khatri
  • 1,253
  • 13
  • 29
  • Then, what about `` ? – MKJ Oct 28 '17 at 12:23
  • count the array of questions and do for loop on submit, $count = questions_count; for($j = 1; $j <= $count; $j++){ if(isset($_POST['btn-submit'])) { echo $ufname = trim($_POST['radio.$j.']); } – Ritesh Khatri Oct 28 '17 at 12:27
  • What is `question_count`? Is it the number of rows question displayed? – MKJ Oct 28 '17 at 12:33
  • yep its a count of questions. – Ritesh Khatri Oct 28 '17 at 12:36
  • So how should I count that, there may be more than 2 or 5? Should I use `$i`? – MKJ Oct 28 '17 at 12:37
  • yup, you can use it, because the $i is exactly the count of questions. but check with echo first, – Ritesh Khatri Oct 28 '17 at 12:40
  • There is an error after doing that: `Notice: Undefined index: radio.$j. in C:\wamp64\www\quiz\quiz.php on line 120` – MKJ Oct 28 '17 at 12:42
  • I got the error for https://stackoverflow.com/questions/46989575/selection-in-radio-button-gets-replaced-in-two-divs/46989653#comment80926144_46989653, and now I am ready to accept your answer if you approve the edits I made! Thanks for helping me. – MKJ Oct 28 '17 at 12:49
  • Okay i approved your edits, but it not necessary, because – Ritesh Khatri Oct 28 '17 at 12:52
  • Or you can do just like this [multiple-radio-button-groups-in-one-form] (https://stackoverflow.com/questions/28543752/multiple-radio-button-groups-in-one-form) – Ritesh Khatri Oct 28 '17 at 12:53
  • Thnks buddy. :) – Ritesh Khatri Oct 28 '17 at 12:56
  • Instead of `echo` now I am inserting them to database, but only one answer is inserted that too first check not the other.. – MKJ Oct 28 '17 at 12:59
  • I tried: `$count = $i; for($j = 1; $j <= $count; $j++){ if(isset($_POST['btn-submit'])) { $ufname = trim($_POST['radio'.$j]); $istmt = $user->runQuery("INSERT INTO answer(Ans) VALUES(:user_crn)"); $istmt->bindparam(":user_crn",$ufname); $istmt->execute(); return $istmt;` – MKJ Oct 28 '17 at 13:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/157706/discussion-between-mkj-and-rits). – MKJ Oct 28 '17 at 13:01