MrB
MrB

Reputation: 69

radio button group separated from others using php while loop dosn't work

iam working with question page , every user will show him 2 or more questions each question have 4 answers and 4 (radio button) , every question must be separated from other questions , which basically means ever radio button group must be like so , my problem is like the image bellow (i can't post them all )

see my problem

i have use this code (wasn't work)

$sql2 = "SELECT * FROM questions WHERE subject = 'web lang' LIMIT 5 ";
$result2 = mysqli_query($conn,$sql2);

              echo "<form action='' method='POST'>";

                  while ($row2 = mysqli_fetch_array($result2,MYSQLI_ASSOC)) {

                    echo "<div align='center'>
                    <table>

                      <thead>
                      <tr >
                        <th >Q & A</th>
                        <th>Choose</th>
                      </tr>
                      </thead>
                      <tbody>
                      <tr>
                        <td>".$row2['question']."</td>
                        <td></td>
                      </tr>
                      <tr>
                        <td>".$row2['ans1']."</td>
                        <td><input type='radio' name='chk' value='1' >
                      </tr>
                      <tr>
                        <td>".$row2['ans2']."</td>
                        <td><input type='radio' name='chk' value='2' >
                      </tr>
                      <tr>
                        <td>".$row2['ans3']."</td>
                        <td><input type='radio' name='chk' value='3' >
                      </tr>
                      <tr>
                        <td>".$row2['ans4']."</td>
                        <td><input type='radio' name='chk' value='4' >
                      </tr>
                      </tbody>
                    </table><br> <br>
                    </div>
                    ";

                  }
                  echo "<input type='submit' name='submit'>
                  </form>";
                  if (isset($_POST['submit'])) {

                      $chk = $_POST['chk'];

                      Echo $chk;

                  }

even if i echo like this

print_r(array_values($chk));

and if i use different name each time i cant get the name , i use this code inside while loop , its ok works but how to get value of other tables ?

$new = 0;
<input type='radio' name='chk".$new."' value='some value' >
$new++;

Upvotes: 0

Views: 134

Answers (2)

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

Assuming the fact the you have a questionID column associated with each question, change your while loop in the following way,

// your code
echo "<form action='' method='POST'>";
while ($row2 = mysqli_fetch_array($result2,MYSQLI_ASSOC)) {
    ?>
    <div align='center'>
    <table>
        <thead>
            <tr >
                <th >Q & A</th>
                <th>Choose</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><?php echo $row2['question']; ?></td>
                <td></td>
            </tr>
            <tr>
                <td><?php echo $row2['ans1']; ?></td>
                <td><input type='radio' name='chk[<?php echo $row2['quesrtionID']; ?>]' value='1' >
            </tr>
            <tr>
                <td><?php echo $row2['ans2']; ?></td>
                <td><input type='radio' name='chk[<?php echo $row2['quesrtionID']; ?>]' value='2' >
            </tr>
            <tr>
                <td><?php echo $row2['ans3']; ?></td>
                <td><input type='radio' name='chk[<?php echo $row2['quesrtionID']; ?>]' value='3' >
            </tr>
            <tr>
                <td><?php echo $row2['ans4']; ?></td>
                <td><input type='radio' name='chk[<?php echo $row2['quesrtionID']; ?>]' value='4' >
            </tr>
        </tbody>
    </table>
    <br> <br>
    </div>
    <?php
}
echo "<input type='submit' name='submit'>
</form>";
// your code

Later when the user selects the radio options and hit submit button, fetch the question IDs and the corresponding (given) answer in the following way,

if (isset($_POST['submit'])) {
    foreach($_POST['chk'] as $questionID => $answer){
        // $questionID is the question ID and $answer is
        // the corresponding given answer of that quesrtion
    }
}

Upvotes: 1

Joaquin Peraza
Joaquin Peraza

Reputation: 343

Try this:

$sql2 = "SELECT * FROM questions WHERE subject = 'web lang' LIMIT 5 ";
$result2 = mysqli_query($conn,$sql2);

              echo "<form action='' method='POST'>";
               $i=0;
                  while ($row2 = mysqli_fetch_array($result2,MYSQLI_ASSOC)) {

                    echo "<div align='center'>
                    <table>

                      <thead>
                      <tr >
                        <th >Q & A</th>
                        <th>Choose</th>
                      </tr>
                      </thead>
                      <tbody>
                      <tr>
                        <td>".$row2['question']."</td>
                        <td></td>
                      </tr>
                      <tr>
                        <td>".$row2['ans1']."</td>
                        <td><input type='radio' name='chk".$i."' value='1' >
                      </tr>
                      <tr>
                        <td>".$row2['ans2']."</td>
                        <td><input type='radio' name='chk".$i."' value='2' >
                      </tr>
                      <tr>
                        <td>".$row2['ans3']."</td>
                        <td><input type='radio' name='chk".$i."' value='3' >
                      </tr>
                      <tr>
                        <td>".$row2['ans4']."</td>
                        <td><input type='radio' name='chk".$i."' value='4' >
                      </tr>
                      </tbody>
                    </table><br> <br>
                    </div>
                    ";
                   $i++;
                  }
                  echo "<input type='submit' name='submit'>
                  </form>";
                  if (isset($_POST['submit'])) {

                      $chk = $_POST['chk'];

                      Echo $chk;

                  }

Upvotes: 1

Related Questions