473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Simple questionnaire validation problem

12
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\htdocs\dissertation\v41\questionnaire.p hp on line 118

Notice: Undefined index: 1 in c:\Project\htdocs\dissertation\v41\questionnaire.p hp on line 119

Notice: Undefined index: 2 in c:\Project\htdocs\dissertation\v41\questionnaire.p hp on line 118

Notice: Undefined index: 2 in c:\Project\htdocs\dissertation\v41\questionnaire.p hp 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($check_answers);

// Loop all the questions and check that answers were submitted
while ($row = mysql_fetch_array($check_answers_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($_REQUEST['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><font color=blue>In Section 2:</font> You forgot to make a selection for <font color=red>Q".$item[$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><font color=blue>In Section 2:</font> You forgot to make a selection for <font color=red>Q".$item[$qid]."![/PHP]

Any help would be great please

Thanks

Nabeel
Apr 3 '07 #1
7 2068
code green
1,726 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. 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_array($check_answers_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($_REQUEST['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?
Apr 3 '07 #2
javedna
12
Expand|Select|Wrap|Line Numbers
  1. 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_array($check_answers_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($_REQUEST['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
Apr 3 '07 #3
code green
1,726 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. Secondly it does work it for some reason 
  2. in the error message it states the qid inside the item is undefined. 
  3. This is only for the else part of my statement and 
  4. 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.
Apr 4 '07 #4
javedna
12
Expand|Select|Wrap|Line Numbers
  1. Secondly it does work it for some reason 
  2. in the error message it states the qid inside the item is undefined. 
  3. This is only for the else part of my statement and 
  4. 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
Apr 4 '07 #5
code green
1,726 Expert 1GB
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.
Expand|Select|Wrap|Line Numbers
  1. $check_answers_result = @mysql_query($check_answers);
DO NOT use the '@'. It is hiding your possible php errors. Change this to
[PHP]if(!$check_answers_result = mysql_query($check_answers)){
echo 'Query failed '.$check_answers;
echo mysql_error();
}[/PHP]For a start.Then look at your result set and check you have something
[PHP]while ($row = mysql_fetch_array($check_answers_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
Expand|Select|Wrap|Line Numbers
  1. if (!empty($_REQUEST['item[$qid]'])) {    
  2.        $item[$qid] = $_REQUEST['item[$qid]'];
Change to
[PHP]if(isset($_REQUEST['item'])){
$postArray = $_REQUEST['item'];
if (!empty($postArray)) {
if (isset($postArray[$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
Apr 5 '07 #6
javedna
12
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_answers_result = mysql_query($check_answers)){
echo 'Query failed '.$check_answers;
echo mysql_error();
}

// Loop all the questions and check that answers were submitted
while ($row = mysql_fetch_array($check_answers_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($_REQUEST['item'])) {
$postArray = $_REQUEST['item'];
if (!empty($postArray)) {
if (isset($postArray[$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\htdocs\dissertation\v44\questionnaire.p hp on line 109
Error2Array ( [0] => 2 [question_id] => 2 )
Notice: Undefined index: item in c:\Project\htdocs\dissertation\v44\questionnaire.p hp on line 109
Error2Array ( [0] => 3 [question_id] => 3 )
Notice: Undefined index: item in c:\Project\htdocs\dissertation\v44\questionnaire.p hp on line 109
Error2Array ( [0] => 4 [question_id] => 4 )
Notice: Undefined index: item in c:\Project\htdocs\dissertation\v44\questionnaire.p hp on line 109
Error2Array ( [0] => 5 [question_id] => 5 )
Notice: Undefined index: item in c:\Project\htdocs\dissertation\v44\questionnaire.p hp on line 109
Error2Array ( [0] => 6 [question_id] => 6 )
Notice: Undefined index: item in c:\Project\htdocs\dissertation\v44\questionnaire.p hp 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
Apr 8 '07 #7
code green
1,726 Expert 1GB
The following is wrong[PHP]if (!isset($_REQUEST['item'])) {
$postArray = $_REQUEST['item'];[/PHP]This should be [PHP]if (isset($_REQUEST['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
Apr 10 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Jason A. Thompson | last post by:
Dear Access Gurus, I have a database which I hoped to use to administer questionnaires, or rather that someone who knows nothing about Access could use to administer them. Each q'aire item is on...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
3
by: Tom_F | last post by:
To comp.databases.ms-access -- I have a questionnaire for which I would like to design a MIcrosoft Access form. I understand that the proper Access table structure would be: Respondent_ID ...
0
by: Christian Zotter | last post by:
Hello NG, I am a student at UDA in Austria, Austrian Partner of International Universities. To finnish my study i have to do a final year project. I have choosen the topic 'Evaluation of low...
1
by: javedna | last post by:
Can PHP help with the following as I have tried in the MYSQL Forums and cant get any help Thanks Nabz ---------------------------------------- Hi I am developing a PHP MYSQL questionnaire...
2
by: Vili | last post by:
Hi all I am having problems with creating an functional questionnaire with asp.net 2.0 and MSSQL 2005 database. I have a table with field id (key & auto int), clientId (int), QuestionId...
10
by: fishlab | last post by:
Hello I'm using visual basic 2005 and i'm really useless and completely stuck. i'm making a questionnaire with a track bar from 1-7 to collect reponses. I have three forms. The first form...
20
by: gremlin | last post by:
http://www.cilk.com/multicore-blog/bid/6703/C-Inventor-Bjarne-Stroustrup-answers-the-Multicore-Proust-Questionnaire
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.