views:

44

answers:

2

Hello all. I am trying to create a jquery questionnaire but my little jquery knowledge doesn't help me.

What I have until now is the following http://valogiannis.com/stackoverflow/quest.html

When you click Questions a popup open with two questions.

I want the following: when the user click one of the two answer, the script check his answer. The array CheckFirstAnswer is responsible to "tell" if the first answer is wrong or correct, 0 for wrong, 1 for correct. If user click the correct answer then I want to appear the next question with its answers from the arrays Question,FirstAnswer,SecondAnswer otherwise the corresponding Conclusion proportionately the i value.

I would appreciate any help.

Thanks

A: 

You should use $.ajax to send back the answer so that the server can evaluate the answer and send the next question. Currently, any one with a basic knowledge of JS can see the CheckFirstSnswer array and know the correct answer. You have an HTML page on the site, hence I don't know the server side programming language you are using.

Make a new page that accepts posts from this page and evaluates the request. This page will send the question id, answer/s selected and auth'ed userid to the new page. The new page will maintain the score at server side and send the next questions

Ravindra Sane
+2  A: 

[Working demo]

Question manager

// question number
var currentQ = -1;

function showNewQuestion(el) {
    currentQ++; // increment question number
    $('.messagepop').html( Question[currentQ] + '<br />' + 
                          '<a href="#" class="first">' 
                             + FirstAnswer[currentQ] + '</a><br />' + 
                          '<a href="#" class="second">' 
                             + SecondAnswer[currentQ] + '</a><br />' + 
                          '<a href="#" class="close">close</a>' );
}

function validate(answer) {
    var firstIsTrue = CheckFirstAnswer[currentQ];

    // correct answer (new question)
    if (  firstIsTrue && answer == 1
      || !firstIsTrue && answer == 2 ) {
      showNewQuestion(); 
    } 

    // incorrect answer (conclusion)
    else  {
       $('.messagepop').html(Conclusion[currentQ]);
    }
}

Click handler

$(function () {

  $("#container_div").live('click', function (event) {

    // which element was clicked
    var el = $(event.target);

    // first answer was clicked
    if (el.hasClass("first")) {
        validate(1);
    } 

    // second answer was clicked
    else if (el.hasClass("second")) { 
        validate(2);
    } 

    // questions opener was clicked
    else if (el.attr("id") == "questions") {
        el.addClass("selected").parent()
          .append('<div class="messagepop pop" />');
        showNewQuestion();
        $(".pop").slideFadeToggle();
    } 

    // popup close was clicked
    else if (el.hasClass("close")) {
        $(".pop").slideFadeToggle();
        $("#questions").removeClass("selected");
    }
  });
});
galambalazs
:O you @galambalazs! you are awesome! http://www.youtube.com/watch?v=CYzVL0vI-kI
Sotiris
wow, thanks. :)
galambalazs