I'm very much depressed with the following code. As for me, everything is correct. But the Goddamn code doesn't work.
I've a html form with radio buttons and a hidden field. On clicking Submit, the form does not submit. Trying print_r($_POST) on the submitted radio button variable, it returns an empty array ie., Array(). Below is my complete code.
/* quiz.php */
<?php
session_start();
if(isset($_SESSION['email']))
{
$qid=$_SESSION["quesno"]+1;
$_SESSION["quesno"]=$qid;
include('database_access_param.php');
$question="";
if($qid<=10)
{
$db_link=mysql_connect($hostname, $dbuser, $dbpassword) or die("Unable to connect to the server!");
mysql_select_db($dbname) or die("Unable to connect to the database.");
$sql = "select * from questions where qid=".$qid;
$result=mysql_query($sql);
$row = mysql_fetch_row($result);
$qid=$row[0];
$question=$row[1];
$answer1=$row[2];
$answer2=$row[3];
$answer3=$row[4];
$answer4=$row[5];
$rtanswer=$row[6];
print('<html>');
print('<head><title>');
print('Quiz 1</title>');
print('<link rel="stylesheet" href="report.css">');
print("</head>");
print('<body>');
print('<form name=myform method="post" action="save_grade.php">');
print('<table class="report" align=left width=100%>');
print('<tr><th align=left colspan=2>Quiz</th></tr>');
print('<tr><td> </td></tr>');
?>
<tr><td><input type="hidden" name="right" value="<?php echo $rtanswer; ?>"></td></tr>
<?
print('<tr><td>'.$qid.')'.' '.$question.'</td></tr>');
?>
<tr><td><input type="radio" name="answer" value="1"> <?php echo $answer1; ?></td></tr>
<tr><td><input type="radio" name="answer" value="2"> <?php echo $answer2; ?></td></tr>
<tr><td><input type="radio" name="answer" value="3"> <?php echo $answer3; ?></td></tr>
<tr><td><input type="radio" name="answer" value="4"> <?php echo $answer4; ?></td></tr>
<tr><td> </td></tr>
<tr><td><input type="submit" name="submit" value="Next"> <input type=reset name=reset value="Clear" ></td></tr></table>
<?
print('<br><br><br><p>You cannot go back and resubmit an answer.'.' '.'System counts every submit and it affects your score</p>');
print('</form>');
print('</body>');
print('</html>');
}
else
{
header("Location:view_scores.php");
}
}
else
{
header("Location:index.php");
}
?>
/* save_grade.php */
<?php
session_start();
if(isset($_SESSION['email']))
{
if(isset($_POST["answer"])) // problem in this line. print_r() returns empty Array()
{
$answer=$_POST["answer"];
$right=$_POST["right"];
if($answer==$right)
{
$_SESSION["score"]=$_SESSION["score"]+1;
}
header("Location:quiz.php");
}
else
{
print_r($_POST);
print('cannot submit!!');
}
}
else
{
header("Location:index.php");
}
?>