Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

HELP!!!!!!!!!!!!!!!! Quiz with both radio and checkboxes

Options
  • 14-04-2011 6:35pm
    #1
    Banned (with Prison Access) Posts: 46


    Making a quiz and need to have both radio buttons and checkboxes within the same quiz but when i have it to check the answers the question is not checked and just ignored. Im using an array to hold the correct answers but with the checkbox questions, iv realised i cant hold two values in it. Can someone please help?


Comments

  • Registered Users Posts: 213 ✭✭Hoku


    Assuming that the quiz is written in PHP, you need to pass the checkboxes as an array.
    http://www.kavoir.com/2009/01/php-checkbox-array-in-form-handling-multiple-checkbox-values-in-an-array.html

    If you Google "php form multiple checkboxes", you will get numerous articles with advice on how to handle the checkboxes.


  • Banned (with Prison Access) Posts: 46 louie12ie


    Sorry its XHTML and javascript. Sorry didnt mention earlier


  • Registered Users Posts: 213 ✭✭Hoku


    There is only so much that Javascript can do to a form....
    In the end it has to go to a processing script, be it ASP, Perl or PHP.

    Anyway, try this guide here:
    http://www.javascript-coder.com/javascript-form/javascript-get-check.phtml

    You can probably validate the checkboxes in groups using the || operator, to make sure that at least one within a block is checked.


  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    There's always a way if you're willing to write extra code.
    Here's something made with the "I don't care anymore, just make it do that thingy I want" approach.
    You could work backwards from here and make the function reusable... then see how the inputs it needs can be worked into the overall structure of the quiz.
    Maybe the array of answers has a key for whether that question is radio or checkbox, then hand it off to the appropriate function. Any answer-checking function returns true or false and you tally the score as you loop.
    <html>
    <head>
    <script type="text/javascript">
    function getAnswers()
    {
      var q5numOptions = 5;
      var q5answers = new Array(1,5);
      var userAnswers = new Array();
      
      for(var i=1; i <= q5numOptions; i++)  {
        if(document.getElementById('q5a'+i).checked)  userAnswers.push(i);
      }
      
      if(q5answers[0] == userAnswers[0] && q5answers[1] == userAnswers[1])
    	alert('Correct');
      else
        alert('Wrong');
    }
    </script>
    </head>
    <body>
    <p>Which of these things are animals?</p>
    Dog  <input type="checkbox" id="q5a1"><br>
    Cup  <input type="checkbox" id="q5a2"><br>
    Can  <input type="checkbox" id="q5a3"><br>
    Tree <input type="checkbox" id="q5a4"><br>
    Cow  <input type="checkbox" id="q5a5"><br>
    <input type="submit" onClick="getAnswers()">
    </body>
    </html>
    


Advertisement