473,382 Members | 1,425 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,382 software developers and data experts.

Undefined index and constants question

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
4 1982
@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
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
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
8,658 Expert Mod 8TB
you break the programme flow. further, an undefined variable may later cause a real error.
Apr 15 '10 #5

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

Similar topics

1
by: D. Alvarado | last post by:
Hello, Ia m running PHP 4 on Fedora Linus Apache 1.27. Since I have a dev box, I would like warnings to appear in the event of undefined variables or constants. But although I specified this...
6
by: Wayne Wengert | last post by:
I have an ASP page in which I reference quite a few ADO constants such as adOpenStatic, adLockReadOnly and they work fine but a reference to adUseServer comes up as undefined? I have a typelib...
1
by: Foolster41 | last post by:
I'm rather new to C++ programing. I'm using the dev-C++ program on a windows XP OS. I'm trying to compile the code for a multi user dungeon (MUD) called circle-mud. When I compile I get the...
9
by: petermichaux | last post by:
Hi, I am curious about how php deals with the following situation where I use an undefined index into an array. PHP seems to be behaving exactly how I want it to but I want to make sure that it...
14
by: Craig O'Shannessy | last post by:
Hi all, Just thought I'd mention that I really think this problem needs to be fixed. I I'm patching the 7.4RC1 JDBC drivers as we speak due to this optimiser bug, and it's the third time...
6
by: mark_galeck_spam_magnet | last post by:
Hello, K&R say about the constant expression after #if: "The resulting constant expression is restricted: it must be integral (...)" Clearly, the expression 1 < FOO if FOO is undefined, is...
5
by: Miquel van Smoorenburg | last post by:
I have a database with a btree index on the 'removed' field, which is of type 'date'. However it isn't being used: techdb2=> explain select * from lines where removed > CURRENT_DATE; QUERY PLAN...
4
by: John Oliver | last post by:
PHP Notice: Undefined index: name in /home/www/reformcagunlaws.com/new.php on line 6 PHP Notice: Undefined index: address in /home/www/reformcagunlaws.com/new.php on line 7 PHP Notice: ...
26
by: Craig Morrison | last post by:
I'm getting this: PHP Notice: Undefined index: D1 in /var/www/html..... From this line of code: $id_option = $_POST; Which is posted by this:
45
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.