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>&nbsp;</td></tr>');

?>

<tr><td><input type="hidden" name="right" value="<?php echo $rtanswer; ?>"></td></tr>

<?

print('<tr><td>'.$qid.')'.'&nbsp;&nbsp;&nbsp;'.$question.'</td></tr>');

?>

<tr><td><input type="radio" name="answer" value="1">&nbsp;&nbsp;&nbsp;<?php echo $answer1; ?></td></tr>
<tr><td><input type="radio" name="answer" value="2">&nbsp;&nbsp;&nbsp;<?php echo $answer2; ?></td></tr>
<tr><td><input type="radio" name="answer" value="3">&nbsp;&nbsp;&nbsp;<?php echo $answer3; ?></td></tr>
<tr><td><input type="radio" name="answer" value="4">&nbsp;&nbsp;&nbsp;<?php echo $answer4; ?></td></tr>

<tr><td>&nbsp;</td></tr>

<tr><td><input type="submit" name="submit" value="Next">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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");
}
?>

Recommended Answers

All 5 Replies

one breakthrough in the above code. The form is submitting if the form method=GET and i get the required output. But the form is not submitting if the form method=POST

change all {name="answer"} to {name="answer[]"} and try

one more thing- convert all <? (short tags) to <?php

nope...that does not work.

first thing first, you can embed php in html,

$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];

?>


close, the php tags there only, and let html be displayed as html..

<html>
<head>,

second thing, you are using mysqli in your database, either use pdo, for database fetch..

try that..

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.