this is my code in firstpage

<?php 
require 'dataconnection.php';

$res = mysql_query("select * from questions where category_id=$category ORDER BY RAND() LIMIT 20") or die(mysql_error());
                    $rows = mysql_num_rows($res);


          echo $rows;
          $i=1;
           while($result=mysql_fetch_assoc($res)){




               if($i==1){?>
                       <div id='question<?php echo $i;?>' class='cont'>

                    <p class='questions' id="qname<?php echo $i;?>"> <?php echo $i?>.<?php echo $result['Question'];?></p>
                    <input type="radio" value="A" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo $result['QNo'];?>'/><?php echo $result['Option A'];?>
                   <br/>
                    <input type="radio" value="B" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo  $result['QNo'];?>'/><?php echo $result['Option B'];?>
                    <br/>
                    <input type="radio" value="C" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo $result['QNo'];?>'/><?php echo $result['Option C'];?>
                    <br/>
                    <input type="radio" value="D" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo $result['QNo'];?>'/><?php echo $result['Option D'];?>
                    <br/>
                    <input type="radio" checked='checked' style='display:none' value="5" id='radio1_<?php echo $result['QNo'];?>' name=''/>                                                                      
                    <br/> 

                <input type="hidden" id="offset" value='<?php echo $i+1; ?>' />
                    <button id='next<?php echo $i;?>' class='next btn btn-success' type='button'>Next</button>


                 </div>    

                   <?php  }

                     if($i<$rows){?>
                       <div id='question<?php echo $i;?>' class='cont'>

                    <p class='questions' id="qname<?php echo $i;?>"> <?php echo $i?>.<?php echo $result['Question'];?></p>
                    <input type="radio" value="A" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo $result['QNo'];?>'/><?php echo $result['Option A'];?>
                   <br/>
                    <input type="radio" value="B" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo  $result['QNo'];?>'/><?php echo $result['Option B'];?>
                    <br/>
                    <input type="radio" value="C" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo $result['QNo'];?>'/><?php echo $result['Option C'];?>
                    <br/>
                    <input type="radio" value="D" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo $result['QNo'];?>'/><?php echo $result['Option D'];?>
                    <br/>
                    <input type="radio" checked='checked' style='display:none' value="5" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo $result['QNo'];?>'/>                                                                      
                    <br/>                    <input type="hidden" id="offset" value='<?php echo $i+1; ?>' /> 
                    <button id='pre<?php echo $i;?>' class='previous btn btn-success' type='button'>Previous</button> 

                    <input type="hidden" id="offset" value='<?php echo $i+1; ?>' /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

                    <button id='next<?php echo $i;?>' class='next btn btn-success' type='button'>Next</button>


                 </div>    

                   <?php  }elseif($i==$rows){?>
                     <div id='question<?php echo $i;?>' class='cont'>
                    <p class='questions' id="qname<?php echo $i;?>"> <?php echo $i?>.<?php echo $result['Question'];?></p>
                    <input type="radio" value="A" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo  $result['QNo'];?>'/><?php echo $result['Option A'];?>
                   <br/>
                    <input type="radio" value="B" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo  $result['QNo'];?>'/><?php echo $result['Option B'];?>
                    <br/>
                    <input type="radio" value="C" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo  $result['QNo'];?>'/><?php echo $result['Option C'];?>
                    <br/>
                    <input type="radio" value="D" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo  $result['QNo'];?>'/><?php echo $result['Option D'];?>
                    <br/>
                    <input type="radio" checked='checked' style='display:none' value="5" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo $result['QNo'];?>'/>                                                                      
                    <br/>              

                    <input type="hidden" id="offset" value='<?php echo $i+1; ?>' /> 
                    <button id='pre<?php echo $i;?>' class='previous btn btn-success' type='button'>Previous</button> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                    <button id='next<?php echo $i;?>' class='next btn btn-success' type='submit'>Finish</button>
                    </div>
          <?php } $i++;   $_SESSION['questions'][]=$result;
            }
          ?>

second page code.

<div>
<?php

for($i=0;$i<20;$i++) { ?>
<div id='questions'  class='container-fluid cont'>


                    <p class='questions' id="qname<?php echo $i;?>"> <?php echo $i+1?>.<?php echo $_SESSION['questions'][$i]['Question'];?></p>
                    <input type="radio" value="A" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo $_SESSION['questions'][$i]['QNo'];?>'/><?php echo $_SESSION['questions'][$i]['Option A'];?>
                   <br/>
                    <input type="radio" value="B" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo $_SESSION['questions'][$i]['QNo'];;?>'/><?php echo $_SESSION['questions'][$i]['Option B'];?>
                    <br/>
                    <input type="radio" value="C" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo $_SESSION['questions'][$i]['QNo'];;?>'/><?php echo $_SESSION['questions'][$i]['Option C'];?>
                    <br/>
                    <input type="radio" value="D" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo $_SESSION['questions'][$i]['QNo'];;?>'/><?php echo $_SESSION['questions'][$i]['Option D'];?>
                    <br/>
                    <input type="radio" checked='checked' style='display:none' value="5" id='radio1_<?php echo $_SESSION['questions'][$i]['QNo'];;?>' name='<?php echo $result['QNo'];?>'/>                                                                      
                    <br/> 
                </div>



<?php } 

it give the values that are stored in session variable.but doesnot gives the selectd radio button values.please help me , how to this.

Dani AI

Generated

Short summary and practical fix for : the selected radio values aren’t arriving because only inputs that are inside a submitted form and that have a proper name are sent to PHP — a radio group submits a single name=value pair (or nothing if nothing is checked). Use one form that contains every question (even if you hide/show questions with JS), give each question’s radios the same name (for example answers[123] where 123 is the question id), and then read $_POST['answers'] on the next page. (developer.mozilla.org)

Minimal implementation pattern (keeps navigation via JS, but submits all answers together). Wrap all question blocks in one form, call session_start() before using $_SESSION, and use integer QNo keys so you get $_POST['answers'] as an associative array:

<?php
session_start(); // must be before any output
// $questions is an array of question rows saved earlier
?>
<form method="post" action="submit.php">
<?php foreach ($questions as $q): ?>
  <fieldset>
    <legend><?php echo htmlspecialchars($q['Question']); ?></legend>
    <?php foreach (['A','B','C','D'] as $opt): ?>
      <label>
        <input type="radio"
               name="answers[<?php echo (int)$q['QNo']; ?>]"
               value="<?php echo $opt; ?>"
               <?php if (!empty($_SESSION['answers'][$q['QNo']]) && $_SESSION['answers'][$q['QNo']] === $opt) echo 'checked'; ?>>
        <?php echo htmlspecialchars($q["Option{$opt}"]); ?>
      </label>
    <?php endforeach; ?>
  </fieldset>
<?php endforeach; ?>
<button type="submit">Finish</button>
</form>

On submit.php, validate and normalize the posted values, then store or evaluate them. Cast question IDs to (int), whitelist allowed values (A,B,C,D), and escape anything you echo back with htmlspecialchars(). Also stop using the old mysql_* API — it was deprecated/removed; use PDO or mysqli and prepared statements instead (as suggested). Example processing skeleton:

<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['answers'])) {
  $allowed = ['A','B','C','D'];
  $safe = [];
  foreach ($_POST['answers'] as $qno => $val) {
    $q = (int)$qno;
    if (in_array($val, $allowed, true)) $safe[$q] = $val;
  }
  $_SESSION['answers'] = $safe;
  // store/evaluate with PDO prepared statements
}

Notes: don’t hard-code a 20-item loop on the display page — use count($_SESSION['questions']) so you don’t run into undefined indexes. Avoid “hidden checked” radios to force a default; treat a missing answers[QNo] key as unanswered on the server. Call session_start() early and use PDO/mysqli for DB work. (php.net)

Member Avatar for Member #120589

Urgh. Could you try to mix markup and code to a greater extent?! This is very difficult to read, even moreso to maintain.
Tip - use PDO/mysqli - mysql_* functions are dead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.