0

I have created a quiz website combining sql,php and html. the questions are fetched from the database. My *.js code and *.php code is below

 <script>
$(document).ready(function(){
        $('body').on('click','input[type="radio"]', function(){
            var curr_id = $('.question').data('nextQuestion');
            var answer1 = $('#radio1').is(':checked');
            var answer2 = $('#radio2').is(':checked');
            var answer3 = $('#radio3').is(':checked');
            var answer4 = $('#radio4').is(':checked');
            getCorrectAnswer(curr_id, answer1, answer2, answer3, answer4);
   setTimeout(getQuestion.bind(this,curr_id, answer1, answer2, answer3, answer4), 1000);

    });

    function getQuestion(curr_id, answer1=false, answer2=false, answer3=false, answer4=false){
  console.log(curr_id);
        $.post("ajax.php",
        {
            next_id: parseInt(curr_id)+1,
            answer1: answer1,
            answer2: answer2,
            answer3: answer3,
            answer4: answer4,
        },
        function(data, status){
            $('#container_for_questions').html(data);
        });
    }

    function getCorrectAnswer(curr_id, answer1=false, answer2=false, answer3=false, answer4=false){
        $.post("ajax_get_correct_answer.php",
        {
            next_id: parseInt(curr_id),
            answer1: answer1,
            answer2: answer2,
            answer3: answer3,
            answer4: answer4,
        },
        function(data, status){
            $('#container_for_questions').html(data);
        });
    }

    getQuestion(-1);
});

</script>
<?php
// Start the session
session_start();

$con=mysqli_connect("localhost","root","","quiz"); // change here to your data
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// Check the number of all questions, if next_id is more than last question, back to first or whatever you want;
$response=mysqli_query($con,"select * from questions");
$number_of_all_questions = mysqli_num_rows($response);

if($_POST['next_id'] == 0){
 // reset to default
 $_SESSION["correct_score"] = 0;
 $_SESSION["not_correct_score"] = 0;
}


if($number_of_all_questions <= $_POST['next_id']){
 // Quiz finished, show results
    echo"<div>
 <h2>Results:</h2>
 <p>Correct answers: {$_SESSION['correct_score']}</p>
 <p>Wrong answers: {$_SESSION['not_correct_score']}</p>

 </div>";



}else{

 // query next question
 $response=mysqli_query($con,"select * from questions WHERE id =(select min(id) from questions where id > {$_POST['next_id']})");
 ?>

 <?php while($result=mysqli_fetch_array($response,MYSQLI_ASSOC)){ ?>

  <div id="question_<?= $result['id'] ?>" class='question' data-next-question="<?= $_POST['next_id'] ?>"> <!--check the class for plurals if error occurs-->
   <h2><?= $result['id'].".".$result['question_name'] ?></h2>
   <div class='align'>
    <input type="radio" value="1" id='radio1' name='1'>
    <label id='ans1' for='radio1'><?= $result['answer1'] ?></label>
    <br/>
    <input type="radio" value="2" id='radio2' name='2'>
    <label id='ans2' for='radio2'><?= $result['answer2'] ?></label>
    <br/>
    <input type="radio" value="3" id='radio3' name='3'>
    <label id='ans3' for='radio3'><?= $result['answer3'] ?></label>
    <br/>
    <input type="radio" value="4" id='radio4' name='4'>
    <label id='ans4' for='radio4'><?= $result['answer4'] ?></label>
   </div>
   <br/>
   <?php /*<input type="button" data-next-question="<?= $_POST['next_id'] ?>" id='next' value='Next!' name='question' class='butt'/> */?>
  </div>
 <?php }?>
<?php }?>
<?php mysqli_close($con); ?>

But when I run this this is the error I get:

"ErrorException Undefined index: next_id".

at HandleExceptions->handleError(8, 'Undefined index: next_id', 'C:\xampp\htdocs\np\app\ajax_get_correct_answer.php', 28, array('con' => object(mysqli))) i dont much about php. can anyone help

Amiga500
  • 5,874
  • 10
  • 64
  • 117

1 Answers1

0

Replace Below

if($_POST['next_id'] == 0){
    // reset to default
    $_SESSION["correct_score"] = 0;
    $_SESSION["not_correct_score"] = 0;
}

to // Check if next_id is set in Post from ajax. if set reset to default(correct_score,not_correct_score)

if(isset($_POST['next_id']) && $_POST['next_id'] == 0){
    // reset to default
    $_SESSION["correct_score"] = 0;
    $_SESSION["not_correct_score"] = 0;
}

You can use below suggestion.

// declare variable on top

 $next_id=0;
 if(isset($_POST['next_id']) && $_POST['next_id'] != 0){
    $next_id=$_POST['next_id'];
    }

Later where ever u see $_POST['next_id'] replace to $next_id

Null Pointer
  • 458
  • 1
  • 4
  • 14
  • but the this line is showing next_id not defined "if($number_of_all_questions <= $_POST['next_id'])" –  May 22 '18 at 09:22
  • @kingkhan i gave him answer where is going wrong he can then make code changes as per his need. There are many ways to write code. – Null Pointer May 22 '18 at 09:25
  • can you edit the code completely in your way –  May 22 '18 at 09:30
  • i have edited evrything as you said . the question is displayed but its not moving to the next question –  May 22 '18 at 09:59
  • do you think i should change anything in my javascript code in the question. can you check –  May 22 '18 at 10:04