Help in quiz with php and jquery

hi there i am trying to develop a quiz using php and jquery with next question and show one question per page and when use id done show him the results

here is my code i want help plz

    <html>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    		<script src="js/jquery-1.10.2.min.js"></script>
    		<!-- Include all compiled plugins (below), or include individual files as needed -->
    		<script src="js/bootstrap.min.js"></script>
    		<script src="js/jquery.validate.min.js"></script>
    		
    		<script>
    		$('.cont').addClass('hide');
    		count=$('.questions').length;
    		 $('#question'+1).removeClass('hide');
    		 
    		 $(document).on('click','.next',function(){
    		     last=parseInt($(this).attr('id'));     
    		     nex=last+1;
    		     $('#question'+last).addClass('hide');
    		     
    		     $('#question'+nex).removeClass('hide');
    		 });
    		 
    		 $(document).on('click','.previous',function(){
                 last=parseInt($(this).attr('id'));     
                 pre=last-1;
                 $('#question'+last).addClass('hide');
                 
                 $('#question'+pre).removeClass('hide');
             });
             
             
             
    		</script>
    <head>
    <style>
    .container {
    				margin-top: 110px;
    			}
    			.error {
    				color: #B94A48;
    			}
    			.form-horizontal {
    				margin-bottom: 0px;
    			}
    			.hide{display: none;}
    header{
    	background: #339966;
    	height: 70px;
    	color:#fff;
    }
    a{text-decoration: none;color: #fff;}
    header p{padding-top:20px;}
    footer{
    	background: #339966;
    	height: 100px;
    	color:#fff;
    }
    footer p{padding-top:50px;}
    .image{
    	padding-top:10%;
    	height: 453px;
    }
    .intro{
    	margin:150px 0px 150px 70px;
    }
    .question{height: 342px;}
    .result-logo{margin-left: 42%;margin-top:1.6%;}
    .result-logo1{margin-left: 55%;}
    .result-container{margin-left: 40%;margin-top:1%; color:#684B68;}
    .logout{padding-top:100px;}
    .next{margin-left:200px;}
    .answer{color:green;font-weight: 300;font-size: larger; }
    .result{height: 452px;}
    </style>
    </head>
    <?php
    // This script displays the quiz selected
    
    // Display the header
    
    // concatenate the target path from the parent dir path
    
    
    $quiz = $_GET['q'];
    //$this_dir = dirname(__FILE__);
    //$parent_dir = realpath($this_dir . '../');
    //$target_path = $parent_dir . '/tests/';
    $quizfile =$quiz.'.qz';
    if (file_exists($quizfile)){
    include(htmlspecialchars($quizfile));
    echo "<h2>$title</h2>";
    }else{ 
    die();
    }
    ?>
    Answer these questions as honestly as you can.  If you think more than one applies to you, just try
    to pick the one that <i>most</i> applies to you.<BR><BR>
    <center>
    <form action="quizresults.php?quiz=<? echo $quiz ?>" method=post>
    <table border=0 width="100%">
    <?
    
    // Show all the questions and allow the user to select answers
    //for ($i=0; $i<$questions; $i+=1)
    
    //$i=0;
      if($i==0){?> 
    <div id='question<?php echo $i=0;?>' class='cont'>
     <p class='questions' id="<?php echo $i;?>"></p>
    	<tr>
          <td colspan=2 bgcolor="#999999"><font color="#000000"><? echo $question[$i]; ?></font></td>
        </tr>
    <?
    	for ($j=0; $j<$types; $j+=1)
    	{
    ?>
    		<tr>
    			<td valign=top width=1><input type="radio" name="q_<? echo $i+1; ?>" value="<? echo $j+1; ?>">
    			<td><? echo $answer[$i][$j]; ?></td>
    		</td></tr>
    <?
    	}
      
     
    ?>
    <input id="<?php echo $i+=1;?>"type="button" onclick="<?php echo $i+1;?>" class='next btn btn-success' ></center>
    </div>
    </TABLE>
    <table border=0 width="100%">
    
    <?php 
    // Show all the questions and allow the user to select answers
    //for ($i=0; $i<$questions; $i+=1)
      }else if ($i>1) { ?>
       <div id='question<?php echo $i+=1;?>' class='cont'>
        <p class='questions' id="qname<?php echo $i;?>"></p>
    <?php echo $i;?>
    	<tr>
          <td colspan=2 bgcolor="#999999"><font color="#000000"><? echo $question[$i]; ?></font></td>
        </tr>
    <?
    	for ($j=0; $j<$types; $j+=1)
    	{
    ?>
    		<tr>
    			<td valign=top width=1><input type="radio" name="q_<? echo $i+1; ?>" value="<? echo $j+1; ?>">
    			<td><? echo $answer[$i][$j]; ?></td>
    		</td></tr>
            </div>
    <?
    	}
    }
    ?>
    <input type="button" value="next"></center>
    </TABLE>
    <BR><BR>
    <input type="submit" value="عرض النتجة!"></center>
    </form>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    		<script src="js/jquery-1.10.2.min.js"></script>
    		<!-- Include all compiled plugins (below), or include individual files as needed -->
    		<script src="js/bootstrap.min.js"></script>
    		<script src="js/jquery.validate.min.js"></script>
    		
    		<script>
    		$('.cont').addClass('hide');
    		count=$('.questions').length;
    		 $('#question'+1).removeClass('hide');
    		 
    		 $(document).on('click','.next',function(){
    		     last=parseInt($(this).attr('id'));     
    		     nex=last+1;
    		     $('#question'+last).addClass('hide');
    		     
    		     $('#question'+nex).removeClass('hide');
    		 });
    		 
    		 $(document).on('click','.previous',function(){
                 last=parseInt($(this).attr('id'));     
                 pre=last-1;
                 $('#question'+last).addClass('hide');
                 
                 $('#question'+pre).removeClass('hide');
             });
             
             
             
    		</script>


this script load file called with gz like this : 
<?
$title="test from lawrence";

$types="3";
$questions="3";

$type[0] = "<p>hero</p>";
$type[1] = "<p>good</p>";
$type[2] = "<p>bad</p>";

$question[0] = "what's your name ?";
$answer[0][0] = "power";
$answer[0][1] = "safe";
$answer[0][2] = "no";

$question[1] = "what's your color?";
$answer[1][0] = "fire";
$answer[1][1] = "light";
$answer[1][2] = "red";

$question[2] = "what's your mood?";
$answer[2][0] = "angry";
$answer[2][1] = "cute";
$answer[2][2] = "nerv";
?>

i wanrt to make it show one question per page with next question thanks

Oh it’s Quiz time of the year again already? Man the school year just flies by…

So you’re doing a lot of stuff here. Also i think you pasted your code more than once. That or you’re including your javascript files multiple times.

Are you trying to use the Jquery to load all the questions, and then only SHOW one at a time? or do you want each pageload to load a single question?

i want to show one question on a page then the user click on next button to go to next question thanks

A year ago I wrote a online trivia game just for fun and used jquery, php and Ajax. Anyways, I fould it easier to put all the jQuery towards the bottom of the HTML. Like so:

<?php

require('lib/includes/utilities.inc.php');

unset($_SESSION['current_id']); // Forces user back to first question:
$pageTitle = "Online Movie Trivia";
include 'lib/includes/header.inc.php';
?>

<div class="container span5 mainContent">
	<!-- The Question that is pulled in from PHP and jQuery -->
	<div class="questStyle">
		<h3 class="displayQuest">&nbsp;</h3>
	</div>
	<!-- The Possible Answers that is also pulled in from PHP and jQuery -->
	<ol class="possibleAnswers">
		<li><a class="answer1 row span12 clicked" href="#" data-answer="1"></a></li>
		<li><a class="answer2 row span12 clicked" href="#" data-answer="2"></a></li>
		<li><a class="answer3 row span12 clicked" href="#" data-answer="3"></a></li>
		<li><a class="answer4 row span12 clicked" href="#" data-answer="4"></a></li>
	</ol>
	<br>
	<div class="actionBar">
		<button class="nextBtn">Next Question</button>
	</div>

	<?php if ($pageTitle == "Online Movie Trivia") { ?>
	<div class="scoreboard">
		<!-- The Countdown Counter done in jQuery -->
		<div class="stopwatch">
			<p class="countdownClock">Time Left: <span class="timer"></span></p>
		</div>
		<!-- The Score done in jQuery -->
		<div class="scoreBox">
			<p class="score">Points: <span class="totalPoints"></span> </p>
		</div>  
	</div>	
	<?php } ?>		

</div>

	<footer class="container span5 page-footer">
		<h4 class="footerHeader">&copy; 2014 J.R. Pepp - All Rights Reserved</h4>
	</footer>
  <!-- The jQuery Library and the external jQuery scripts -->
	<script src="lib/js/jquery-1.10.2.min.js"></script> 
	<script src="lib/js/movieTrivia.01.01.06.2014.js"></script>
  <!--<script src="lib/js/save.js"></script>
  <script src="lib/js/edit.js"></script>
  <script src="lib/js/register.js"></script>-->
  <!--<script src="lib/js/movieTrivia.ver.1.52.js"></script>-->
</body>
</html>

An I tried to keep most of the php at the top of the page. Anyways, as you can see the HTML is pretty simple when it comes to making a quiz game. I think straightening out the main HTML page is the key and that is what I would do. I be glad to help you with the jQuery, if you want.

1 Like

Hello Pepster, can you paste the source of the quiz script? Or at least can yo help pe to link the questions from the database with jquery to frontend?

I am trying to make a quiz with multiple choices (meaning 1, 2 or even 3 correct answers) but i don’t know how to display the questions and build the script correctly.

Here is a sample question in the database:

“id”: 1
“question”: “some text”,
“a”: “var1”,
“b”: “var2”,
“c”: “var3”,
“pic”: “1.jpg”,
“correct”: “a”,

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.