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

Undefined index and constants question

P: 5
I recently started to learn PHP and a lot of this is starting to make sense, once in a while I get stump and confused at why some things just donít work the way the book I am following works out.

This week I started to learn about forms and how to use different methods of building them out.

This example is from my book and even though the actions and results are what the book says they should, I get an error message that I just canít figure it out how to fix it.

I am using PHP 5.3.0 / apache 2.2.11 / mysql 5.1.36 on my test server

This is my html code:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Feet to Meters Comversion Tool</title>
  4. </head>
  5. <body>
  6. <?php
  7. //check to see if the form has been submitted
  8. $feet = $_GET['feet'];
  9. if ($_GET['feet'] != NULL) {
  10. echo "<strong>$feet </strong> feet converts to <strong>";
  11. echo $feet * 0.3048;
  12. echo "</strong> meters.<br />";
  13. }
  14. ?>
  15. <form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="GET">
  16. <label>Feet:
  17. <input type="text" name="feet" value="<?php echo $feet; ?>" />
  18. </label>
  19. <input type="submit" value="Convert!" />
  20. </form>
  21. </body>
  22. </html>


When I open the form with my browser (IE, Mozilla, and Chrome) it get this error message:


Notice: Undefined index: feet in /localhost/example2.php on line 10



After entering a value to calculate, the form process the input and presents me the correct product. The form does what it is supposed to do and there is no problems I can see with the functionality of it. I just would like to know what the error is about and how to correct it.

Thanks for your help,
Apr 14 '10 #1
Share this Question
Share on Google+
4 Replies


guillermobytes
P: 77
@PHPWanabe
the error means that you are trying to assign some key's value of some array that is not defined.
lets say you have an array like this:
Expand|Select|Wrap|Line Numbers
  1. $array = array('a'=>'value1', 'b'=>'value2', 'c'=>'value3');
if you try to access the first value like this:
Expand|Select|Wrap|Line Numbers
  1. $array['a'];
everything works fine because it is defined, however if you try to assign the value of some undefined key (like lets say 'z') it will output a notice like the one you showed:
Expand|Select|Wrap|Line Numbers
  1. $feet = $array['z']; //outputs : Notice undefined index...
to avoid this problem you should always check if the key is set before trying to access it, like this:
Expand|Select|Wrap|Line Numbers
  1. if (isset($array['z'])) {
  2.     $zValue = $array['z'];
  3. } else {
  4.     //'z' key is not defined, do something else
  5. }
to solve your porblem in your script you should do this:
Expand|Select|Wrap|Line Numbers
  1. //check to see if the form has been submitted
  2. if (isset($_GET['feet'])) {
  3. $feet = $_GET['feet'];
  4. } else {
  5. $feet = 'the get variable \'feet\' was not submitted';
  6. }
Apr 14 '10 #2

P: 5
Thank you for your help. Specially for the explanation.

I finally got the form working free of warnings and error messages.

Thanks, appreciate the help


This is the final code. Maybe somebody else can use this example.



Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Feet to Meters Comversion Tool</title>
  4. </head>
  5. <body>
  6. <?php
  7. // using error reporting to clean code
  8. error_reporting(E_ALL);
  9. //check to see if the form has been submitted
  10. if (isset($_GET['feet'])) {
  11. $feet = $_GET['feet'];
  12. echo "<strong>$feet </strong> feet converts to <strong>";
  13. echo $feet * 0.3048;
  14. echo "</strong> meters.<br />";
  15. }
  16. else {
  17. // set variable to 'empty' so the input box is not populated when accessing the form for the first time
  18. $feet = '';
  19. }
  20. ?>
  21. <form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="GET">
  22. <label>Feet:
  23. <input type="text" name="feet" value="<?php echo $feet; ?>" />
  24. </label>
  25. <input type="submit" value="Convert!" />
  26. </form>
  27. </body>
  28. </html>
Apr 14 '10 #3

P: 5
I have one more question:

What is the caveat of just letting the error checking ignore those warning messages. Specifically the one I was having? Is there a security risk from having that malformed form? (No, I didn’t thought about making it rime)

The form does what it is supposed to do and it has no other problems.
Apr 14 '10 #4

Dormilich
Expert Mod 5K+
P: 8,639
you break the programme flow. further, an undefined variable may later cause a real error.
Apr 15 '10 #5

Post your reply

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