如何将选定的单选按钮值保存到php中的下一页?

I am doing an online quiz application, i need to keep all selected radio button values to next page when i click the next button,becoz am using pagination.In this following code when am moving to next page it shows undefined offset errors .I got one more syntax error like unexpected checked in this line />'.$optA[$i].' i need solution for these problems please help anyone,thanks in advance

 <?php
    session_start();
    include('connection.php');
    mysql_select_db('quiz_apptitude');
    $sql = mysql_query("SELECT * FROM prob_on_age");
    $nr = mysql_num_rows($sql); 
    if (isset($_GET['pn'])) {
        $pn = preg_replace('#[^0-9]#i', '', $_GET['pn']); 
    } else { 
        $pn = 1;
    } 

    $itemsPerPage = 2; 

    $lastPage = ceil($nr / $itemsPerPage);

    if ($pn < 1) {
        $pn = 1; 
    } else if ($pn > $lastPage) { 
        $pn = $lastPage; 
    } 

    $centerPages = "";
    $sub1 = $pn - 1;
    $sub2 = $pn - 2;
    $add1 = $pn + 1;
    $add2 = $pn + 2;
    if ($pn == 1) {
        $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
        $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
    } else if ($pn == $lastPage) {
        $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
        $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    } else if ($pn > 2 && $pn < ($lastPage - 1)) {
        $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '">' . $sub2 . '</a> &nbsp;';
        $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
        $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
        $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
        $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '">' . $add2 . '</a> &nbsp;';
    } else if ($pn > 1 && $pn < $lastPage) {
        $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
        $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
        $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
    }

    $limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage; 

    $sql2 = mysql_query("SELECT * FROM prob_on_age $limit") or die('error'); 
    //$total=mysql_num_rows($sql2) ;
    $i=0;
    $outputList = '';
    while($row = mysql_fetch_array($sql2)){ 

       $Question[]=$row['prob_Question'];
    $optA[]=$row['prob_OptionA'];
    $optB[]=$row['prob_OptionB'];
    $optC[]=$row['prob_OptionC'];
    $optD[]=$row['prob_OptionD'];
    $ans[]=$row['prob_Answer'];
    $Qid[$i]=$row['prob_Qid'];

        $ans[$i]=$row['prob_Answer'];
                         $r=$i+1;




        $outputList .= '<table><tr>
                  <td> '.$r.' . '.$Question[$i].'</td>
                </tr>
                <tr>  <td><input type="hidden" name="'.$Qid[$i].'" id="'.$Qid[$i].'" value="'.$Qid[$i].'" /></td>
                </tr>
                  <tr>

                  <td><input type="radio"  name=" '.$Qid[$i].'" id="'.$Qid[$i].'"  value="'.$optA[$i].'" <?php echo '.$optA[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : ''; ?>/> 
                    '.$optA[$i].'</td></tr>
                    <tr> <td><input type="radio" name=" '.$Qid[$i].'" id="'.$Qid[$i].'" value="'.$optB[$i].'" <?php echo '.$optB[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : ''; ?> />
                    '.$optB[$i].'</td></tr>
                 <tr> <td><input type="radio" name=" '.$Qid[$i].'"  id="'.$Qid[$i].'" value="'.$optC[$i].'"  <?php echo '.$optC[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : ''; ?>/>
                   '.$optC[$i].'</td></tr>
                <tr>  <td><input type="radio" name=" '.$Qid[$i].'" id="'.$Qid[$i].'"  value="'.$optD[$i].'"  <?php echo '.$optD[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : ''; ?>/>
                '.$optD[$i].'</td></tr>

                <tr>  <td><input type="hidden" name="'.$ans[$i].'" id="'.$Qid[$i].'"  value="'.$ans[$i].'" /></td>
                </tr></table>';

    $i++;
    }

    $count=count($Qid);
    $paginationDisplay = ""; 
    if ($lastPage != "1"){

        $paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. '&nbsp;  &nbsp;  &nbsp; ';

        if ($pn != 1) {
            $previous = $pn - 1;

            $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> ';
        } 

        $paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';

        if ($pn != $lastPage) {
            $nextPage = $pn + 1;

            $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '">Next</a> ';
        } 
    }

    ?>

<?php
session_start();


include('connection.php');
mysql_select_db('quiz_apptitude');
$sql = mysql_query("SELECT * FROM prob_on_age");
$nr = mysql_num_rows($sql); 
if (isset($_GET['pn'])) {
    $pn = preg_replace('#[^0-9]#i', '', $_GET['pn']); 
} else { 
    $pn = 1;
} 

$itemsPerPage = 2; 

$lastPage = ceil($nr / $itemsPerPage);

if ($pn < 1) {
    $pn = 1; 
} else if ($pn > $lastPage) { 
    $pn = $lastPage; 
} 

$centerPages = "";
$sub1 = $pn - 1;
$sub2 = $pn - 2;
$add1 = $pn + 1;
$add2 = $pn + 2;
if ($pn == 1) {
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
} else if ($pn == $lastPage) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
} else if ($pn > 2 && $pn < ($lastPage - 1)) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '">' . $sub2 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '">' . $add2 . '</a> &nbsp;';
} else if ($pn > 1 && $pn < $lastPage) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
}

$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage; 

$sql2 = mysql_query("SELECT * FROM prob_on_age $limit") or die('error'); 
//$total=mysql_num_rows($sql2) ;
$i=0;
$outputList = '';
while($row = mysql_fetch_array($sql2)){ 

   $Question[]=$row['prob_Question'];
$optA[]=$row['prob_OptionA'];
$optB[]=$row['prob_OptionB'];
$optC[]=$row['prob_OptionC'];
$optD[]=$row['prob_OptionD'];
$ans[]=$row['prob_Answer'];
$Qid[$i]=$row['prob_Qid'];

    $ans[$i]=$row['prob_Answer'];
                     $r=$i+1;




    $outputList .= '<table><tr>
              <td> '.$r.' . '.$Question[$i].'</td>
            </tr>
            <tr>  <td><input type="hidden" name="'.$Qid[$i].'" id="'.$Qid[$i].'" value="'.$Qid[$i].'" /></td>
            </tr>
              <tr>

              <td><input type="radio"  name=" '.$Qid[$i].'" id="'.$Qid[$i].'"  value="'.$optA[$i].'" <?php echo '.$optA[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : ''; ?>/> 
                '.$optA[$i].'</td></tr>
                <tr> <td><input type="radio" name=" '.$Qid[$i].'" id="'.$Qid[$i].'" value="'.$optB[$i].'" <?php echo '.$optB[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : ''; ?> />
                '.$optB[$i].'</td></tr>
             <tr> <td><input type="radio" name=" '.$Qid[$i].'"  id="'.$Qid[$i].'" value="'.$optC[$i].'"  <?php echo '.$optC[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : ''; ?>/>
               '.$optC[$i].'</td></tr>
            <tr>  <td><input type="radio" name=" '.$Qid[$i].'" id="'.$Qid[$i].'"  value="'.$optD[$i].'"  <?php echo '.$optD[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : ''; ?>/>
            '.$optD[$i].'</td></tr>

            <tr>  <td><input type="hidden" name="'.$ans[$i].'" id="'.$Qid[$i].'"  value="'.$ans[$i].'" /></td>
            </tr></table>';

$i++;


}

$count=count($Qid);








$paginationDisplay = ""; 
if ($lastPage != "1"){

    $paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. '&nbsp;  &nbsp;  &nbsp; ';

    if ($pn != 1) {
        $previous = $pn - 1;

        $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> ';
    } 

    $paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';

    if ($pn != $lastPage) {
        $nextPage = $pn + 1;

        $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '">Next</a> ';
    } 
}

?>

    <html>
    <head>
    <title>online QiuZ</title>
    </head>
    <body>
       <div style="margin-left:64px; margin-right:64px;">
         <h2>Problem On Age</h2>
       </div> 
       <!--   <div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php //echo $paginationDisplay; ?></div>-->
          <div style="margin-left:64px; margin-right:64px;">
          <form name="prob_age" id="prob_age" method="post" action="result-age.php" >
          <?php print "$outputList"; ?>

          <div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div>
          <input type="submit" name="submit" id="submit" value="submit">

          </form>
          </div>

    </body>
    </html>

As I have check the code, I found that your variable $output list data is not correct. replace that by following:

You are echoing in the string of the php variable with the if condition:

<?php echo '.$optB[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : '';

The above line you are using in the $output variable that is not correct. You need to change this line as following:

$output .= ....//other code
$output .= ($optB[$i] == $_POST[$Qid[$i]]) ? ' checked="checked"' : '';
$output .= ......

Replace your each <?php echo in the string of $output variable as above.

EDITED:

Check you below code:

$outputList .='<table><tr>
              <td> '.$r.' . '.$Question[$i].'</td>
               </tr>
               <tr>  <td><input type="hidden" name="'.$Qid[$i].'" id="'.$Qid[$i].'" value="'.$Qid[$i].'" /></td>
            </tr>
              <tr>

              <td><input type="radio"  name=" '.$Qid[$i].'" id="'.$Qid[$i].'"  value="'.$optA[$i].'" 

<?php echo '.$optA[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : ''; ?>

//here you are starting the php tag again in within php tag. so remove the php and echo tags. and use as i have mention use this as $outputList .= ($optA[$i] == $_POST[$Qid[$i]]) ? ' checked="checked"' : ''; and close the above sting before it. and do it similarly for other.

/> 
                '.$optA[$i].'</td></tr>
                <tr> <td><input type="radio" name=" '.$Qid[$i].'" id="'.$Qid[$i].'" value="'.$optB[$i].'" <?php echo '.$optB[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : ''; ?> />
                '.$optB[$i].'</td></tr>
             <tr> <td><input type="radio" name=" '.$Qid[$i].'"  id="'.$Qid[$i].'" value="'.$optC[$i].'"  <?php echo '.$optC[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : ''; ?>/>
               '.$optC[$i].'</td></tr>
            <tr>  <td><input type="radio" name=" '.$Qid[$i].'" id="'.$Qid[$i].'"  value="'.$optD[$i].'"  <?php echo '.$optD[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : ''; ?>/>
            '.$optD[$i].'</td></tr>

            <tr>  <td><input type="hidden" name="'.$ans[$i].'" id="'.$Qid[$i].'"  value="'.$ans[$i].'" /></td>
            </tr></table>';

Note: if you are not getting what I am saying then let me know I will post modified code. and will modify more you.

Well, you could do it with PHP Sessions and/or Cookies (almost the same for your case) but if for any reason something goes wrong in the middle of the quiz... you will lose all the progress and the user have to start all over again.

If the quiz is short, you can do it that way, but if it's >= middle or even long one... I will suggest to use $_POST & Forms instead of pagination links. and of course, save the progress in the meanwhile.

With this, if you want to pre-check radio buttons, input field, or anything... just have to call the proper variable next time you open that page (could be also another day because the values are storage in your server and not in user machine.)
good luck