By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,816 Members | 2,151 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,816 IT Pros & Developers. It's quick & easy.

Php script is producing an error "undefined index: question"

P: 16
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.  
  4.  /*the points system for this Q&A has to be passed from question to question.  
  5. We are going
  6. to do this by passing it in the URL.  After the first question, the current score is
  7.  passed
  8. through in the URL for the form POST,*/ 
  9.  
  10. $question1="
  11. <b>Question 1: What is the URL to access the PHP users manual online?</b><br>
  12. A) <input type=radio name='q1a' value='a'> http://www.php.org/docs.php<br>
  13.  
  14. B) <input type=radio name='q1a' value='b'> http://www.php.com/docs.php<br>
  15.  
  16. C) <input type=radio name='q1a' value='c'> http://www.php.net/docs.php<br>
  17.  
  18. D) <input type=radio name='q1a' value='d'> http://documentation.php.net<br>
  19.  
  20.  
  21. <input type=submit value='Go'>
  22.  
  23. </form>";
  24. $question1_answer="c";
  25.  
  26.  
  27. $question2="
  28. <b>Question 2: What is the extention that PHP documents generally have?</b><br>
  29.  
  30. A) <input type=radio name='q2a' value='a'> .phps<br>
  31.  
  32. B) <input type=radio name='q2a' value='b'> .html<br>
  33.  
  34. C) <input type=radio name='q2a' value='c'> .htm<br>
  35.  
  36. D) <input type=radio name='q2a' value='d'> .php<br>
  37.  
  38.  
  39. <input type=submit value='Go'>
  40.  
  41.  
  42. </form>";
  43.  
  44. $question2_answer="d";
  45.  
  46.  
  47. //the final bit in here is a review of each question, and the correct answer(s) for that question.
  48.  
  49. $results=$question1."<br><b>Correct answer: $question1_answer</b><br><i>Why: The other domains go somewhere else.</i><br><br>";
  50.  
  51. $results.=$question2."<br><b>Correct answer: $question2_answer</b><br><i>Why: .php is the default extention the server is setup for, 
  52.                                                                          tho any extention can be setup to parse as a PHP script</i><br><br>"; 
  53.  
  54. $results.="Ok, now I understand, take me <a href='hello.php'>back to the beginning</a> to try again.";
  55.  
  56.  
  57.  
  58.  
  59.  
  60. //the code below is where am having a problem am getting an error "undefined index error:question"
  61. switch($_GET['question']) 
  62. {
  63.  
  64.     case "1":
  65.  
  66.                  print "<form method=post action='hello.php?question=2'>$question1
  67.  
  68. </form>";
  69.  
  70. }
  71. ?>
Am a beginner in programming trying to learn php on my own.As i have commented on the last piece of code, the switch statement that is where my problem is am getting an error saying "undefined index:question" and the word "question" is found on my switch condition statement. i think there is a problem in the way i write my form statement inside my switch statement.please help!!
Sep 29 '11 #1
Share this Question
Share on Google+
10 Replies


Rabbit
Expert Mod 10K+
P: 12,359
It means that there is no key with the name question being passed in the GET array when the page is being loaded.
Sep 29 '11 #2

P: 16
what does it mean by saying there is no key?
Oct 6 '11 #3

Rabbit
Expert Mod 10K+
P: 12,359
When the form submitted, there was nothing on there called question.
Oct 6 '11 #4

P: 16
so what was i suppose to do?
Oct 7 '11 #5

Rabbit
Expert Mod 10K+
P: 12,359
Pass in a field named question from your form.
Oct 7 '11 #6

P: 16
Thank you, but how do i do that if i have already declared my question to be $quetion1 and i want it to be displayed on the screen so that user on answering he should prompt question2 later on .Please sorry coz my problem is that am just a beginner in programing.
Oct 7 '11 #7

Rabbit
Expert Mod 10K+
P: 12,359
But that's the thing. I don't see any indication that you've declared the question to be question 1. You've defined a variable to hold question 1 but no where in the code does it say that the default question is question 1. Nor have you indicated that you've passed in question 1 in the first place. In the absensce of this, I have to assume that there is no question parameter passed in.
Oct 7 '11 #8

P: 16
okay thank you for hint, but can you help maybe by a code example to elaborate to me what you mean by no question parameters passed in plz!
Oct 10 '11 #9

Rabbit
Expert Mod 10K+
P: 12,359
If the URL I use is www.mysite.com/index.php?question=1
then this won't give an error $_GET['question'].

However, if the URL I use is www.mysite.com/index.php
then this will give me the error you're getting $_GET['question'].

In the first example, I passed in the 'question' variable through GET. In the second example, there is no 'question' variable, so the PHP code errors out.
Oct 10 '11 #10

P: 16
hey rabbit thank you very much for the explanation i have understood the concept and corrected it.Passing variables by post method i have been trying to use this but never really understood it well, thank you again.Hopefully you will still support me for my up coming question.
Oct 11 '11 #11

Post your reply

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