ajay.motah 0 Newbie Poster

Hello the code below will fetch all unaswered questions from the db. But when there is only one question, when next is done, it doest submit, and gives a blank page.If more than 1 questions it works fine. Please help. Thank you

<?php 
session_start();

//uri get from index
$qu=$_GET['qu'];
$userid=$_SESSION['userid'];
if(!isset($_SESSION['username']))
    {
        header('location:../index.php');


    }
    include('../includes/config.php');
    $course_id=$_SESSION['courseid']
    ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="style.css" rel='stylesheet' type="text/css"/>
<script src="../js/jquery.js"></script>
<!--<script src="js/watch.js"></script>-->
<script>
$(document).ready(function(){

    var steps = $('form').find(".questions");
    var count = steps.size();
    steps.each(function(i){
        hider=i+2;
        if (i == 0) {   
            //$("#question_" + hider).hide();
            createNextButton(i);
        }
        else if(count==i+1){
            var step=i + 1;
            //$("#next"+step).attr('type','submit');
            $("#next"+step).on('click',function(){
            //question_id=$('h2').attr('id');
               submit();

            });
        }
        else{
            //$("#question_" + hider).hide();
            createNextButton(i);
        }

    });
    function submit(){

        //alert(question_id);
        answer=$('input[type="radio"]:checked').val();
         $.ajax({
                        type: "POST",
                        url: "ajax2.php",
                        //data:{question_id:question_id,answer:answer},
                        data: $('form').serialize(),
                        success: function(msg) {
                            //alert (question_id)
                          $("#quiz_form,#demo1").addClass("hide");
                          $('#result').show();
                          $('#result').append(msg);
                        }
         });


    }
    function createNextButton(i){
        var step = i + 1;
        var step1 = i + 2;
        $('#next'+step).on('click',function(){
            alert(step+' '+step1);
            $("#question_" + step).hide();
            $("#question_" + step1).show();
        });
    }


});
</script>
</head>

<body>
<?php
function check_img($var)
    {
        if($var==1)
            {
                echo '<img src="">';
            }


    }




$query=mysql_query("SELECT course_name FROM course WHERE course_id='$course_id'");
$row=mysql_fetch_array($query);

?>
<h1>Exam - <?php echo $row['course_name'];  ?></h1>
<?php $response=mysql_query("SELECT DISTINCT exam.*,questions.*,users.* FROM exam,questions,users WHERE exam.question_id=questions.question_id AND exam.exam_answer='U' AND exam.user_id=users.user_id AND exam.user_id=$userid");
$no_question=mysql_num_rows($response);
$start=1;

?>

<form method='post' id='quiz_form'>

<?php while($result=mysql_fetch_array($response)){ ?>
<div id="question_<?php echo $start;?>" class='questions'>
<h2 id="<?php echo $result['question_id'];?>"><?php echo $result['question'];?></h2>
<div class='align'>

<input type="hidden" name="question_<?php echo $start;?>"  id="question" value="<?php echo $result['question_id'];?>" />


<input type="radio" value="A" id='radio1_<?php echo $start;?>' name='<?php echo $start;?>'>
<label id='ans1_<?php echo $start;?>' for='1'><?php
if($result['img']==1)
{
    echo'<span id="ans">A</span><img src="'.$result['A'].'" width="100" height="100" >';
}
else
{
 echo '<span id="ans">A</span> '.$result['A'];
}


?></label>
<br />
<input type="radio" value="B" id='radio2_<?php echo $start;?>' name='<?php echo $start;?>'>
<label id='ans2_<?php echo $start;?>' for='1'><?php 
if($result['img']==1)
{
    echo'<span id="ans">B</span> <img src="'.$result['B'].'" width="100" height="100" >';
}
else
{
 echo '<span id="ans">B</span> '.$result['B'];
}

?>


</label>
<br />
<input type="radio" value="C" id='radio3_<?php echo $start;?>' name='<?php echo $start;?>'>
<label id='ans3_<?php echo $start;?>' for='1'><?php if($result['img']==1)
{
    echo'<span id="ans">C</span><img src="'.$result['C'].'" width="100" height="100" >';
}
else
{
 echo '<span id="ans">C</span> '.$result['C'];
}?></label>
<br />
<input type="radio" value="D" id='radio4_<?php echo $start;?>' name='<?php echo $start;?>'>
<label id='ans4_<?php echo $start;?>' for='1'><?php if($result['img']==1)
{
    echo'<span id="ans">D</span> <img src="'.$result['D'].'" width="100" height="100" >';
}
else
{
 echo '<span id="ans">D</span> '.$result['D'];
}?></label>
<br />
<input type="radio" value="E" id='radio5_<?php echo $start;?>' name='<?php echo $start;?>'>
<label id='ans5_<?php echo $start;?>' for='1'><?php if($result['img']==1)
{
    echo'<span id="ans">E</span> <img src="'.$result['E'].'" width="100" height="100" >';
}
else
{
 echo '<span id="ans">E</span> '.$result['E'];
}?></label>
<br />

<input type="radio" checked='checked' value="5" style='display:none' id='radio4_<?php echo $start;?>' name='<?php echo $start;?>'>
</div>
<br/>
<input type="button" id='next<?php echo $start;?>' value='Next!' name='question' class='butt'/>
</div>
<?php
$start ++;

 }?>
</form>
<div id='result'>
<img src='results.jpg' alt='Results'/>
<br/>

</div>
</body>
</html>
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.