0

I am making a exam portal in which i have option conducting exam/quiz. Now i have added 2 buttons namely Next and Previous . I wanted to fetch Question and its option on button Click. My database has following structure: Question(qid,question,option1,option2,option3,option4,right_option) What I am tryin to do:

<input id="first" type="button" value="NEXT" onClick = "show_next()">
<input id="second" type="button" value="PREV" onClick = "show_prev()">

<script>
function show_next()
{
<?php 
$question_no; //my global php variable to keep track of question id
$question_no = $question_no + 1; 
show_question($question_no); //php function which shows data according to question no 
?>
 }
function show_prev()
{
<?php 
if($question_no>0)
{
$question_no = $question_no-1;
show_question();
}
else
{
?>
alert("Wrong Operation");
<?php
}
?>
}
</script>

I am new to php and javascript, please suggest the correct method and if possible coding snippet for my question

3
  • What does show_question() function do? It should output a valid Javascript code as string. Commented Apr 10, 2013 at 10:53
  • You can't mix PHP code with Javascript by this way. This alert() in case of failure is in Javascript, but you place it in PHP if...else statement. As PHP finishes job, there are no ifs or elses and this makes no sense. Commented Apr 10, 2013 at 10:55
  • Can you show your show_question() javascript function Commented Apr 10, 2013 at 10:55

2 Answers 2

1

Use jQuery/AJAX. All you have to do is manage Offset and Limit dynamically. e.g.

lets consider you are showing 1 question at a time and its options.

your html file will be.

<input id="first" type="button" value="NEXT" onClick = "show_next()">
<input id="second" type="button" value="PREV" onClick = "show_prev()">
<input id="offset" type="hidden" value="0">

In your javascript file

function show_next()
{
  var offset=$('#offset').val();
  $.post('GetQuestion.php',{offset:offset},function(data){
    $('#question_answer').html(data);
    $('#offset').attr('value',offset+1);

  })
}

function show_prev()
{
  var offset=$('#offset').val();
  $.post('GetQuestion.php',{offset:offset},function(data){
    $('#question_answer').html(data);
    $('#offset').attr('value',offset-1);

  })
}

In your GetQuestion.php file you can access offset value using $_POST. All you have to do is use that value in your query.

mysql_query("SELECT * FROM questions LIMIT ".$_POST['offset'].",1");

echo your query result in php file so that it could be available to var data in javascript.

Sign up to request clarification or add additional context in comments.

3 Comments

my buttons exists in same php file , in this case what modification i should make
also when i try to run the query it says undefined index:offset for the given query . Not able to figure out how to correct it.
" $('#question_answer').html(data);" whose id you are reffering here and what is its use
0

As far as i know, PHP is executed on the server and the JS is executed Client-Side, so you can't mix it.

You have to Options.

  1. Load all Questions in different divs and hide them. Then you can show one by one with the next and the previous buttons.

  2. Build the page with a parameter like this: http://myapp.com/question/1 And make the Next BUtton /question/2 The question page should now contain only 1 question.

Edit:

Here is a fiddle for the 1. method: http://jsfiddle.net/zj7ts/

$('.question').hide()

 $question_shown = $('.question').first().show();

$('.next').click(function(){
    $('.question').hide()
    $question_shown = $question_shown.next().show();
});

$('.prev').click(function(){
    $('.question').hide()
    $question_shown = $question_shown.prev().show();
});

1 Comment

can you tell me how to load all question in divs and hide them or some useful tutorial link for doing so

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.