在PHP中的单选按钮的情况下未定义的索引

时间:2014-04-30 02:41:43

标签: php

我是PHP新手。我正在建立一个在线考试系统。我用单选按钮来回答。它们以这样的方式使用,以便每个问题最终都有一组单选按钮。所以我使用while循环从数据库中检索问题并放置无线电。现在,当我要检查无线电的价值时,它会显示"注意:未定义的索引:ques1" 。

<html>
<head>
<title>EXAM</title>
</head>
<body>
<?PHP
 $i=1;
 $j=1;
 $user_name = "root";
 $password = "";
 $database = "online_exam";
 $server = "127.0.0.1";
 $db_handle = mysql_connect($server, $user_name, $password);
 mysql_connect($server, $user_name, $password);
 $db_found = mysql_select_db($database);
 if ($db_found)
 { session_start();
  if(isset($_POST['b_subject']))
    {
       $selected_button=$_POST['subject'];
       if($selected_button=='subject1')
            $_SESSION['$sub']= "GK";
        else if($selected_button=='subject2')
            $_SESSION['$sub']="math";
        else
            print "error";
    }

      $c = $_SESSION['$cour'];
  $s = $_SESSION['$sub'];
  $SQL = "SELECT ques FROM ques_ans WHERE course='$c' AND subj='$s'";
  $result = mysql_query($SQL);
  while ( $db_field = mysql_fetch_assoc($result) ) 
    {
         print $db_field['ques']."<BR>";
?>
<FORM name ="form1" method ="post"  >  
         <Input type = 'Radio' Name ='ques'.$i value= '1'>YES
         <Input type = 'Radio' Name ='ques'.$i value= '0'>NO
</FORM>        
<?PHP           
          $i++;         
    }     
  mysql_close($db_handle);
}
?>
<FORM name ="form1" method ="post" > 
 <input type="submit" name="submit" id="submit" value="SUBMIT"/>
</FORM>
<?PHP
if (isset($_POST['submit'])) 
{
$selected_radio = $_POST['ques'.$j];
 if ($selected_radio == '1') 
  {
   $ans='1';
  }
 else if ($selected_radio == '0') 
  {
    $ans='0';
  }
print $ans;
}
?>
</body>
</html>

1 个答案:

答案 0 :(得分:3)

Fixed the echo out and you were echoing a seperate form out, all inputs need to be in the same <form>


<html>
<head>
<title>EXAM</title>
</head>
<body>
<?PHP
 $i=1;
 $j=1;
 $user_name = "root";
 $password = "";
 $database = "online_exam";
 $server = "127.0.0.1";
 $db_handle = mysql_connect($server, $user_name, $password);
 mysql_connect($server, $user_name, $password);
 $db_found = mysql_select_db($database);
 if ($db_found)
 { session_start();
  if(isset($_POST['b_subject']))
    {
       $selected_button=$_POST['subject'];
       if($selected_button=='subject1')
            $_SESSION['$sub']= "GK";
        else if($selected_button=='subject2')
            $_SESSION['$sub']="math";
        else
            print "error";
    }

      $c = $_SESSION['$cour'];
  $s = $_SESSION['$sub'];
  $SQL = "SELECT ques FROM ques_ans WHERE course='$c' AND subj='$s'";
  $result = mysql_query($SQL);
echo '<FORM name ="form1" method ="post"  >'; // Echo the form header
  while ( $db_field = mysql_fetch_assoc($result) ) 
    {
         print $db_field['ques']."<BR>";
?>
             <Input type = 'Radio' Name ='ques<?PHP echo $i;?>' value= '1'>YES
             <Input type = 'Radio' Name ='ques<?PHP echo $i;?>' value= '0'>NO       
<?PHP           
          $i++;         
    }     
echo '<input type="submit" name="submit"></form>'; // Close form
  mysql_close($db_handle);
}

if (isset($_POST['submit'])) 
{
$selected_radio = $_POST['ques'.$j];
 if ($selected_radio == '1') 
  {
   $ans='1';
  }
 else if ($selected_radio == '0') 
  {
    $ans='0';
  }
print $ans;
}
?>
</body>
</html>