Simple questionnaire validation problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • javedna
    New Member
    • Mar 2007
    • 12

    Simple questionnaire validation problem

    Hi

    guys Ive got a simple problem, im designing an online questionnaire and on submission the coding that I have used to validate whether a user has filled in all the questions is supposed to identify any questions they have missed out and print out a simple error message.

    It correctly identify which questions have not been filled in but returns the following error message too:

    Notice: Undefined index: 1 in c:\Project\htdo cs\dissertation \v41\questionna ire.php on line 118

    Notice: Undefined index: 1 in c:\Project\htdo cs\dissertation \v41\questionna ire.php on line 119

    Notice: Undefined index: 2 in c:\Project\htdo cs\dissertation \v41\questionna ire.php on line 118

    Notice: Undefined index: 2 in c:\Project\htdo cs\dissertation \v41\questionna ire.php on line 119
    .
    .
    .
    This repeats for all the questions that are not filled in (NULL)!!!



    [PHP] ///////////////////////////////////////////////////////////////////////////////////
    ////////////////////////VALIDATE THE QUESTIONS ANSWERS////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////

    // Validate the The Insertion of all answers -- check that they dont equal NULL

    // Query all the answers
    $check_answers = "SELECT question_id FROM questions as qid ORDER BY question_id ASC";
    $check_answers_ result = @mysql_query($c heck_answers);

    // Loop all the questions and check that answers were submitted
    while ($row = mysql_fetch_arr ay($check_answe rs_result,MYSQL _BOTH)) {

    $qid = $row[0]; // Set the $Question_id which is assigned here to $qid to record 0

    // Check all the answers given match the following
    if (!empty($_REQUE ST['item[$qid]'])) {

    $item[$qid] = $_REQUEST['item[$qid]'];

    if ($item[$qid] =='1') { //answer = 1
    $score1 = '1';
    } elseif ($item[$qid] == '2') { //answer = 2
    $score1 = '2';
    } elseif ($item[$qid] == '3') { //answer = 3
    $score1 = '3';
    } elseif ($item[$qid] == '4') { //answer = 4
    $score1 = '4';
    } elseif ($item[$qid] == '5') { //answer = 5
    $score1 = '5';
    } elseif ($item[$qid] == '6') { //answer = 6
    $score1 = '6';
    } elseif ($item[$qid] == '7') { //answer = 7
    $score1 = '7';
    } elseif ($item[$qid] == '0') { //answer = N/A
    $score1 = '0'; //answer = 7
    }
    } else { //$_REQUEST['question1'] is not set.
    $item[$qid] == NULL;
    $errors[] = "<center><f ont color=blue>In Section 2:</font> You forgot to make a selection for <font color=red>Q".$i tem[$qid]."!</font></center>";
    }
    }[/PHP]

    The questionnaire is dynamic so there are no predefined number of questions. The problem comes from the else part of the statement from here

    [PHP] $item[$qid] == NULL;
    $errors[] = "<center><f ont color=blue>In Section 2:</font> You forgot to make a selection for <font color=red>Q".$i tem[$qid]."![/PHP]

    Any help would be great please

    Thanks

    Nabeel
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Code:
    It correctly identify which questions have not been filled in but returns the following error message too
    I very much doubt anything works in this code.. The error messages are syntax errors so the code obviously isn't working. The following looks very wrong. [PHP]while ($row = mysql_fetch_arr ay($check_answe rs_result,MYSQL _BOTH )) {

    $qid = $row[0]; // Set the $Question_id which is assigned here to $qid to record 0

    // Check all the answers given match the following
    if (!empty($_REQUE ST['item[$qid]'])) {

    $item[$qid] = $_REQUEST['item[$qid]'];[/PHP]Your annotation does not even match what the code is attempting. Remember single quotes are literal strings. And why are you pulling an associative array then using numeric indices. You are using $_REQUEST but I can't see why. Why have you used elseif instead of switch?

    Comment

    • javedna
      New Member
      • Mar 2007
      • 12

      #3
      Originally posted by code green
      Code:
      It correctly identify which questions have not been filled in but returns the following error message too
      I very much doubt anything works in this code.. The error messages are syntax errors so the code obviously isn't working. The following looks very wrong. [PHP]while ($row = mysql_fetch_arr ay($check_answe rs_result,MYSQL _BOTH )) {

      $qid = $row[0]; // Set the $Question_id which is assigned here to $qid to record 0

      // Check all the answers given match the following
      if (!empty($_REQUE ST['item[$qid]'])) {

      $item[$qid] = $_REQUEST['item[$qid]'];[/PHP]Your annotation does not even match what the code is attempting. Remember single quotes are literal strings. And why are you pulling an associative array then using numeric indices. You are using $_REQUEST but I can't see why. Why have you used elseif instead of switch?


      Firstly thanks for replying, Secondly it does work it for some reason in the error message it states the qid inside the item is undefined. This is only for the else part of my statement and not any other which is strange.

      My questionnaire is meant to be dynamic hence the reason for calling the array of answers known as $item the qid inside of this uniquely identifies each answer to each question it works for the submit perfectly and everywhere else that this method has been used in.

      As for my coding style I am relatively new at PHP and MYSQL so apologises if it looks a little inneficient at the moment im getting better at it everyday.

      But back to the problem why does it come out as undefined the number of error messages reduce for the answers I select, so Im on the right track just need to sort out why its undefined in the else part and not anywhere else.

      I look forward to your help again.

      Thanks

      Nabeel

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        Code:
        Secondly it does work it for some reason 
        in the error message it states the qid inside the item is undefined. 
        This is only for the else part of my statement and 
        not any other which is strange.
        IT IS NOT WORKING! The error generated is only of 'notice' level so the script is interpreted and allowed to run. You really need to echo out the array $item[] to have a look at what is in there, then the problem will become apparent.

        Comment

        • javedna
          New Member
          • Mar 2007
          • 12

          #5
          Originally posted by code green
          Code:
          Secondly it does work it for some reason 
          in the error message it states the qid inside the item is undefined. 
          This is only for the else part of my statement and 
          not any other which is strange.
          IT IS NOT WORKING! The error generated is only of 'notice' level so the script is interpreted and allowed to run. You really need to echo out the array $item[] to have a look at what is in there, then the problem will become apparent.
          Again thank you for replying, What do you recommend?, I believe as I am declaring the variable locally inside the if statement it is not considered in the else part. I have tried declaring it outside but it doesnt seem to help!

          Nabz

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            I will try to help you with pleasure, but after today I am offline until Tuesday.
            The problem with PHP is because it is a loosley typed language it allows you to get away with murder. Those coders with a grounding in C/C++ can immediately see why you are having problems. You have not declared any variable in a strictly typed language sense. This just would not compile in C++. I am not criticising you, It is not your fault. What this piece of code needs is some runtime error checking and echoing of variables.
            Code:
            $check_answers_result = @mysql_query($check_answers);
            DO NOT use the '@'. It is hiding your possible php errors. Change this to
            [PHP]if(!$check_answ ers_result = mysql_query($ch eck_answers)){
            echo 'Query failed '.$check_answer s;
            echo mysql_error();
            }[/PHP]For a start.Then look at your result set and check you have something
            [PHP]while ($row = mysql_fetch_arr ay($check_answe rs_result,MYSQL _BOTH)){
            print_r($row);
            if(!empty($row[0]))
            $qid = $row[0];
            else
            echo 'No answer found ';[/PHP]I really do not like this next piece of code for a number of reasons
            Code:
            if (!empty($_REQUEST['item[$qid]'])) {    
                   $item[$qid] = $_REQUEST['item[$qid]'];
            Change to
            [PHP]if(isset($_REQU EST['item'])){
            $postArray = $_REQUEST['item'];
            if (!empty($postAr ray)) {
            if (isset($postArr ay[$qid])) {
            $item[$qid] = $postArray[$qid];
            echo $item[$qid];
            }
            else
            // error
            }
            else
            // error
            }[/PHP]And so forth. I don't have anymore time to spend and I have not proof read the code but I hope you get the idea. CAPTURE ERRORS AND ECHO VARIABLES

            Comment

            • javedna
              New Member
              • Mar 2007
              • 12

              #7
              Thanks very much again for your help,

              I've tried toe code you suggested but unfortunately it still outputs the same problem as my own code, I tried the following code:

              [PHP] // Validate the The Insertion of all answers -- check that they dont equal NULL

              // Query all the answers
              $check_answers = "SELECT question_id FROM questions as qid ORDER BY question_id ASC";

              if(!$check_answ ers_result = mysql_query($ch eck_answers)){
              echo 'Query failed '.$check_answer s;
              echo mysql_error();
              }

              // Loop all the questions and check that answers were submitted
              while ($row = mysql_fetch_arr ay($check_answe rs_result,MYSQL _BOTH)) {
              print_r($row);

              if(!empty($row[0]))
              $qid = $row[0]; // Set the $Question_id which is assigned here to $qid to record 0
              else
              echo 'No answer found ';

              // Check all the answers given match the following
              if (!isset($_REQUE ST['item'])) {
              $postArray = $_REQUEST['item'];
              if (!empty($postAr ray)) {
              if (isset($postArr ay[$qid])) {
              $item[$qid] = $postArray[$qid];
              echo ("Please select an answer for question". $item[$qid]." Please Try again");
              }
              else
              echo ("Error1");
              }
              else
              echo ("Error2");
              }
              }[/PHP]

              the following errors come up:

              Array ( [0] => 1 [question_id] => 1 )
              Notice: Undefined index: item in c:\Project\htdo cs\dissertation \v44\questionna ire.php on line 109
              Error2Array ( [0] => 2 [question_id] => 2 )
              Notice: Undefined index: item in c:\Project\htdo cs\dissertation \v44\questionna ire.php on line 109
              Error2Array ( [0] => 3 [question_id] => 3 )
              Notice: Undefined index: item in c:\Project\htdo cs\dissertation \v44\questionna ire.php on line 109
              Error2Array ( [0] => 4 [question_id] => 4 )
              Notice: Undefined index: item in c:\Project\htdo cs\dissertation \v44\questionna ire.php on line 109
              Error2Array ( [0] => 5 [question_id] => 5 )
              Notice: Undefined index: item in c:\Project\htdo cs\dissertation \v44\questionna ire.php on line 109
              Error2Array ( [0] => 6 [question_id] => 6 )
              Notice: Undefined index: item in c:\Project\htdo cs\dissertation \v44\questionna ire.php on line 109
              Error2Array ( [0] => 7 [question_id] => 7 )

              I removed the line:

              [PHP]$postArray = $_REQUEST['item'];[/PHP]

              The following is output:

              Array ( [0] => 1 [question_id] => 1 ) Array ( [0] => 2 [question_id] => 2 ) Array ( [0] => 3 [question_id] => 3 ) Array ( [0] => 4 [question_id] => 4 ) Array ( [0] => 5 [question_id] => 5 ) Array ( [0] => 6 [question_id] => 6 ) Array ( [0] => 7 [question_id] => 7 ) Array ( [0] => 8 [question_id] => 8 ) Array ( [0] => 9 [question_id] => 9 ) Array ( [0] => 10 [question_id] => 10 ) Array ( [0] => 11 [question_id] => 11 ) Array ( [0] => 12 [question_id] => 12 ) Array ( [0] => 13 [question_id] => 13 ) Array ( [0] => 14 [question_id] => 14 ) Array ( [0] => 15 [question_id] => 15 ) Array ( [0] => 16 [question_id] => 16 ) Array ( [0] => 17 [question_id] => 17 ) Array ( [0] => 18 [question_id] => 18 ) Array ( [0] => 19 [question_id] => 19 ) Array ( [0] => 20 [question_id] => 20 ) Array ( [0] => 21 [question_id] => 21 ) Array ( [0] => 22 [question_id] => 22 ) Array ( [0] => 23 [question_id] => 23 ) Array ( [0] => 24 [question_id] => 24 ) Array ( [0] => 25 [question_id] => 25 ) Array ( [0] => 26 [question_id] => 26 ) Array ( [0] => 27 [question_id] => 27 ) Array ( [0] => 28 [question_id] => 28 ) Array ( [0] => 29 [question_id] => 29 ) Array ( [0] => 30 [question_id] => 30 ) Array ( [0] => 31 [question_id] => 31 ) Array ( [0] => 32 [question_id] => 32 ) Array ( [0] => 33 [question_id] => 33 ) Array ( [0] => 34 [question_id] => 34 ) Array ( [0] => 35 [question_id] => 35 ) Array ( [0] => 36 [question_id] => 36 ) Array ( [0] => 37 [question_id] => 37 ) Array ( [0] => 38 [question_id] => 38 ) Array ( [0] => 39 [question_id] => 39 ) Array ( [0] => 40 [question_id] => 40 ) Array ( [0] => 41 [question_id] => 41 ) Array ( [0] => 42 [question_id] => 42 ) Array ( [0] => 43 [question_id] => 43 ) Array ( [0] => 44 [question_id] => 44 ) Array ( [0] => 45 [question_id] => 45 ) Array ( [0] => 46 [question_id] => 46 ) Array ( [0] => 47 [question_id] => 47 ) Array ( [0] => 48 [question_id] => 48 ) Array ( [0] => 49 [question_id] => 49 ) Array ( [0] => 50 [question_id] => 50 ) Array ( [0] => 51 [question_id] => 51 ) Array ( [0] => 52 [question_id] => 52 ) Array ( [0] => 53 [question_id] => 53 ) Array ( [0] => 54 [question_id] => 54 ) Array ( [0] => 55 [question_id] => 55 ) Array ( [0] => 56 [question_id] => 56 ) Array ( [0] => 57 [question_id] => 57 ) Array ( [0] => 58 [question_id] => 58 ) Array ( [0] => 59 [question_id] => 59 ) Array ( [0] => 60 [question_id] => 60 ) Array ( [0] => 61 [question_id] => 61 ) Array ( [0] => 62 [question_id] => 62 ) Array ( [0] => 63 [question_id] => 63 ) Array ( [0] => 64 [question_id] => 64 ) Array ( [0] => 65 [question_id] => 65 ) Array ( [0] => 66 [question_id] => 66 ) Array ( [0] => 67 [question_id] => 67 ) Array ( [0] => 68 [question_id] => 68 ) Array ( [0] => 69 [question_id] => 69 ) Array ( [0] => 70 [question_id] => 70 ) Array ( [0] => 71 [question_id] => 71 ) Array ( [0] => 72 [question_id] => 72 ) Array ( [0] => 73 [question_id] => 73 ) Array ( [0] => 74 [question_id] => 74 ) Array ( [0] => 75 [question_id] => 75 ) Array ( [0] => 76 [question_id] => 76 )
              I select some of the questions but it does not remove them from the list either!
              any help would be appreciated?

              Nabz

              Comment

              • code green
                Recognized Expert Top Contributor
                • Mar 2007
                • 1726

                #8
                The following is wrong[PHP]if (!isset($_REQUE ST['item'])) {
                $postArray = $_REQUEST['item'];[/PHP]This should be [PHP]if (isset($_REQUES T['item'])) {
                $postArray = $_REQUEST['item'];[/PHP]Then you will see that $_REQUESt['item'] is empty. The problem lies in the HTML of your form wherever that is

                Comment

                Working...