www.webdeveloper.com
Results 1 to 5 of 5

Thread: Multiple Choice-True False Quiz

  1. #1
    Join Date
    Apr 2014
    Posts
    33

    Multiple Choice-True False Quiz

    Hi guys hope your all well,

    I want to put a quiz into my website that I'm doing. I've found some code for a Multiple Choice-True False Quiz

    Here is the code, (yes I know its long and there are some reputation in it) You have to add questions by using a form to add either

    Multiple Choice
    or True False questions this is why there is so much code



    When I try and add a question via the true/false button I get the following error


    Notice: Undefined index: answer3 in C:\xampp\htdocs\quiz\addQuestions.php on line 16

    Notice: Undefined index: answer4 in C:\xampp\htdocs\quiz\addQuestions.php on line 17
    No database selected

    Any help or advice would be much appreciated

    Rich

    ONLY THING IS GUYS MY CODE IS TOO LONG TO POST

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    558
    it means you have a bad index in use with an array variable, or more likely in your situation, you are trying to find an input element in the $_POST array that doesn't exist. When retrieving inputs you must check if the var is set and then begin using it. For ex.:

    PHP Code:
    if (!isset($_POST['answer3']))
        
    $errmsg .= "you must answer question #3";
    else
    {
       
    $answer3 $_POST['answer3'];
       if ((
    $answer3 !== true) || ($answer3 !== false))
           
    $errmsg .= "Invalid answer to question #3";

    PS - we actually prefer when people only post the code needed to document their trouble and to pinpoint where they need help. Glad you didn't post it all, but you could have posted the error lines.

  3. #3
    Join Date
    Apr 2014
    Posts
    33
    Sorry I forgot all about to post the code

    Here is the code that I should've posted, in my mind I'm thinking of simplifying the code by having php code for true/false and multichoice code in separate files just for simplicity

    PHP Code:
    <?php

    if(isset($_POST['desc'])){
        if(!isset(
    $_POST['iscorrect']) || $_POST['iscorrect'] == ""){
            echo 
    "Sorry, important data to submit your question is missing. Please press back in your browser and try again and make sure you select a correct answer for the question.";
            exit();
        }
        if(!isset(
    $_POST['type']) || $_POST['type'] == ""){
            echo 
    "Sorry, there was an error parsing the form. Please press back in your browser and try again";
            exit();
        }
        require_once(
    "quiz/config.php");
        
    $question $_POST['desc'];  

    If (isset(
    $_POST['answer3'])) {$answer3 $POST['answer3'];}

     
    $answer1 $_POST['answer1'];  

     
    $answer2 $_POST['answer2'];  

     
    $answer3 $_POST['answer3'];  

     
    $answer4 $_POST['answer4'];  

     
    $type $_POST['type'];  

     
    $type preg_replace('/[^a-z]/'""$type);  

     
    $isCorrect preg_replace('/[^0-9a-z]/'""$_POST['iscorrect']);  

     
    $answer1 strip_tags($answer1);  

     
    $answer1 mysql_real_escape_string($answer1);  

     
    $answer2 strip_tags($answer2);  

     
    $answer2 mysql_real_escape_string($answer2);  

     
    $answer3 strip_tags($answer3);  

     
    $answer3 mysql_real_escape_string($answer3);  

     
    $answer4 strip_tags($answer4);  

     
    $answer4 mysql_real_escape_string($answer4);  

     
    $question strip_tags($question);  

    $question mysql_real_escape_string($question);  

    //<input name="answer[IDENTIFIER]" value="yourvalue" />

        
    if($type == 'tf'){
        if((!
    $question) || (!$answer1) || (!$answer2) || (!$isCorrect)){
            echo 
    "Sorry, All fields must be filled in to add a new question to the quiz. Please press back in your browser and try again.";
            exit();
            }
        }
        if(
    $type == 'mc'){
        if((!
    $question) || (!$answer1) || (!$answer2) || (!$answer3) || (!$answer4) || (!$isCorrect)){
            echo 
    "Sorry, All fields must be filled in to add a new question to the quiz. Please press back in your browser and try again.";
            exit();
            }
        }
        
    $sql mysql_query("INSERT INTO questions (question, type) VALUES ('$question', '$type')")or die(mysql_error());
            
    $lastId mysql_insert_id();
            
    mysql_query("UPDATE questions SET question_id='$lastId' WHERE id='$lastId' LIMIT 1")or die(mysql_error());
        
    //// Update answers based on which is correct //////////
        
    if($type == 'tf'){
            if(
    $isCorrect == "answer1"){
            
    $sql2 mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer1', '1')")or die(mysql_error());
            
    mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer2', '0')")or die(mysql_error());
            
    $msg 'Thanks, your question has been added';
          
    header('location: addQuestions.php?msg='.$msg.'');
        exit();
        }
        if(
    $isCorrect == "answer2"){
            
    $sql2 mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer2', '1')")or die(mysql_error());
            
    mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer1', '0')")or die(mysql_error());
            
    $msg 'Thanks, your question has been added';
          
    header('location: addQuestions.php?msg='.$msg.'');
        exit();
            }    
        }
        if(
    $type == 'mc'){
            if(
    $isCorrect == "answer1"){
            
    $sql2 mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer1', '1')")or die(mysql_error());
            
    mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer2', '0')")or die(mysql_error());
            
    mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer3', '0')")or die(mysql_error());
            
    mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer4', '0')")or die(mysql_error());
            
    $msg 'Thanks, your question has been added';
          
    header('location: addQuestions.php?msg='.$msg.'');
        exit();
        }
        if(
    $isCorrect == "answer2"){
            
    $sql2 mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer2', '1')")or die(mysql_error());
            
    mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer1', '0')")or die(mysql_error());
            
    mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer3', '0')")or die(mysql_error());
            
    mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer4', '0')")or die(mysql_error());
            
    $msg 'Thanks, your question has been added';
          
    header('location: addQuestions.php?msg='.$msg.'');
        exit();
        }
        if(
    $isCorrect == "answer3"){
            
    $sql2 mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer3', '1')")or die(mysql_error());
            
    mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer1', '0')")or die(mysql_error());
            
    mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer2', '0')")or die(mysql_error());
            
    mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer4', '0')")or die(mysql_error());
            
    $msg 'Thanks, your question has been added';
          
    header('location: addQuestions.php?msg='.$msg.'');
        exit();
        }
        if(
    $isCorrect == "answer4"){
            
    $sql2 mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer4', '1')")or die(mysql_error());
            
    mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer1', '0')")or die(mysql_error());
            
    mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer2', '0')")or die(mysql_error());
            
    mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer3', '0')")or die(mysql_error());
            
    $msg 'Thanks, your question has been added';
          
    header('location: addQuestions.php?msg='.$msg.'');
        exit();
            }
        }
    }
    ?>
    <?php

  4. #4
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    558
    Great - you posted the code. BUT - did you take my suggestion and look thru the code first to see if I had the answer?

  5. #5
    Join Date
    Apr 2014
    Posts
    33
    sorry i didnt i will have a look through it now and get back to you

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles