Preparing an online test in php using javascript

I want to create a test pattern in PHP using a MySQL database. Here I want to get a database of questions and display them on my html pages. Now I want to create a division with the next button, and when the user clicks, he should display the next question, which is dynamically extracted from the database in the same division. I assume this can be achieved via jQquery or javascript, but cannot get the logic.

can anyone help.

early.

here is an example of the code that I tried to display several sections using javascript.

this is my database structure

: qid, question, opt1, opt2, opt3, opt4

this is the php code to get the data form database.

<?php
$result=executeQuery("select * from quest");

if(mysql_num_rows($result)>0){

while($row=mysql_fetch_array($result)){

//echo $row['qid'];echo "<br/>";
    echo $row['question']; echo "<br/>";
    ?>

<input type="radio" name="a1" value="'<?php echo $row['opt1']; ?>'" ><?php echo $row['opt1'];  echo "<br/>"; ?>
<input type="radio" name="a1" value="'<?php echo $row['opt2']; ?>'" ><?php echo $row['opt2']; echo "<br/>";?>
<input type="radio" name="a1" value="'<?php echo $row['opt3']; ?>'" ><?php echo $row['opt3']; echo "<br/>";?>
<input type="radio" name="a1" value="'<?php echo $row['opt4']; ?>'" ><?php echo $row['opt4']; echo "<br/>";?> 



            <?php
}
}

?>

<input type="submit" name="submit" value="submit">

.

javascript

var x=1;
function myfunc(){

    document.getElementById(x).style.display="block" ;
    x++;

}

, , .

+4
2

, Javascript jQuery.

mysql_* API.

, mysql_* mysqli_*:

/* ESTABLISH CONNECTION */
$con = new mysqli("Host", "username", "password", "database"); /* REPLACE NECESSARY DATA INSIDE */

/* CHECK CONNECTION */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

if($stmt = $con->prepare("SELECT qid, question, opt1, opt2, opt3, opt4 FROM quest ORDER BY qid LIMIT 1")){
  $stmt->execute(); /* EXECUTE THE QUERY */
  $stmt->bind_result($qid, $question, $opt1, $opt2, $opt3, $opt4); /* BIND THE RESULT TO THESE VARIABLES */
  $stmt->fetch(); /* FETCH THE RESULT */
  $stmt->close();
} /* END OF PREPARED STATEMENT */

, , .

  <h1 id="question"><?php echo $question; ?></h1>

  <input type="hidden" id="qid" value="<?php echo $qid; ?>">
  <input type="radio" name="a1" id="op1" value="<?php echo $opt1; ?>"><span id="op1text"><?php echo $opt1; ?></span><br/>
  <input type="radio" name="a1" id="op2" value="<?php echo $opt2; ?>"><span id="op2text"><?php echo $opt2; ?></span><br/>
  <input type="radio" name="a1" id="op3" value="<?php echo $opt3; ?>"><span id="op3text"><?php echo $opt3; ?></span><br/>
  <input type="radio" name="a1" id="op4" value="<?php echo $opt4; ?>"><span id="op4text"><?php echo $opt4; ?></span><br/>

  <input type="submit" name="submit" id="submit" value="Next"> <!-- THIS SERVES AS THE SUBMIT AND NEXT BUTTON -->

, .

script, . , AJAX.

<script src="jquery-1.9.1.min.js"></script> <!-- REPLACE NECESSARY JQUERY FILE DEPENDING ON THE VERSION YOU HAVE DOWNLOADED -->
<script>
  $(document).ready(function(){
    $("#submit").click(function(){ /* WHEN SUBMIT IS CLICKED */
      var qid = $("#qid").val(); /* GET THE question id */
      var selected = $("input[type='radio'][name='a1']:checked");
      if (selected.length > 0) { /* CHECK THE SELECTED RADIO BUTTON */
        answer = selected.val();
      }
      $.ajax({
        type: "POST", /* THE METHOD WE WILL BE USING TO PASS THE DATA */
        url: "action.php", /* THIS IS WHERE THE DATA WILL GO */
        data: {"questionid" : qid, "answer" : answer}, /* THE DATA WE WILL BE PASSING */
        dataType : 'json',
        success: function(result){ /* WHEN IT IS SUCCESSFUL */
          /* THIS WILL REPLACE THE DATA IN OUR QUESTION PAGE */
          $("#qid").val(result.questionid);
          $("#question").html(result.question);
          $("#op1").val(result.op1);
          $("#op2").val(result.op2);
          $("#op3").val(result.op3);
          $("#op4").val(result.op4);
          $("#op1text").html(result.op1);
          $("#op2text").html(result.op2);
          $("#op3text").html(result.op3);
          $("#op4text").html(result.op4);
        }
      }); /* END OF AJAX */
    });
  });
</script>

action.php, / .

<?php

  if(isset($_POST["questionid"])){

    /* INCLUDE OUR NEW ESTABLISHED CONNECTION HERE */

    /* PUT HERE YOUR INSERT QUERY WHICH STORES THE USER ANSWERS */

    /* THEN FETCH THE NEXT QUESTION */
    if($stmt = $con->prepare("SELECT qid, question, opt1, opt2, opt3, opt4 FROM quest WHERE qid > ? ORDER BY qid LIMIT 1")){
      $stmt->bind_param("i", $_POST["questionid"]);
      $stmt->execute();
      $stmt->bind_result($qid, $question, $opt1, $opt2, $opt3, $opt4);
      $stmt->fetch();
      $stmt->close();
    } /* END OF PREPARED STATEMENT */

    /* THIS SET OF DATA WILL REPLACE THE DATA IN OUR CURRENT QUESTION PAGE */
    echo json_encode(array("questionid" => $qid, "question" => $question, "op1" => $opt1, "op2" => $opt2, "op3" => $opt3, "op4", => op4));

  } /* END OF ISSET */

?>
+3

ajax. jQuery div. 'click' , .

, . , .

.

while($row=mysql_fetch_array($result)){ ?>
<li class="test " id="qid_<?php echo $row['id']; ?>" >
<?php echo $row['question']; /* here display the stuff */ ?></li>
<?php }

"prev" "next".

<span class="prev_q">Prev</span>      
<span class="next_q">Next</span>

css ( )

li.test { display:none; }
li.activequestion{ display:block;background:#cccccc; color:#c40001; }

script, , ,

<script>
jQuery(document).ready(function(){
jQuery('li.test:first').addClass('activequestion');

jQuery('.next_q').click(function(){
var nonext=jQuery('.test:last').hasClass('activequestion');
if(nonext)
{ alert("no next available"); return false; }
var currentdiv=jQuery('.activequestion').attr('id');
jQuery('.test.activequestion').next().addClass('activequestion');
jQuery('#'+currentdiv).removeClass('activequestion');
});

jQuery('.prev_q').click(function(){
var noprevious=jQuery('.test:first').hasClass('activequestion');
if(noprevious)
{ alert("no previous available"); return false; }
var currentdiv=jQuery('.activequestion').attr('id');
jQuery('.test.activequestion').prev().addClass('activequestion');
jQuery('#'+currentdiv).removeClass('activequestion');
});
});
</script>
-1

Source: https://habr.com/ru/post/1609914/


All Articles