Undefined index and constant question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PHP_Wanabe
Forum Newbie
Posts: 7
Joined: Tue Apr 13, 2010 10:55 pm

Undefined index and constant question

Post by PHP_Wanabe »

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

Code: Select all

<html>
<head>
<title>Feet to Meters Comversion Tool</title>
</head>
<body>
<?php
//check to see if the form has been submitted
$feet = $_GET["feet"];
if ($_GET[feet] != NULL) {
echo "<strong>$feet </strong> feet converts to <strong>";
echo $feet * 0.3048;
echo "</strong> meters.<br />";
}
?>
<form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="GET">
<label>Feet:
<input type="text" name="feet" value="<?php echo $feet; ?>" />
</label>
<input type="submit" value="Convert!" />
</form>
</body>
</html>
When I open the form with my browser (IE, Mozilla, and Chrome) it get this error message followed by the form.

Code: Select all

Notice: Undefined index: feet in C:\localhost\10-8.php on line 8
Notice: Use of undefined constant feet - assumed 'feet' in C:\localhost\10-8.php on line 9
Notice: Undefined index: feet in C:\localhost\10-8.php on line 9

[[b]the form goes here and it displays correctly[/b]]

When I enter a value to convert it into meters [34 feet into meters] it gives me the correct number but I also get this error message:

Code: Select all

Notice: Use of undefined constant feet - assumed 'feet' in C:\localhost\10-8.php on line 9
34 feet converts to 10.3632 meters.
I checked the code, double checked all the spelling and I just don’t know what else to check! I tried using “error_reporting(0);” to suppress the error message and it works but still would like to know what the error is about and how to correct it.

Thanks for your help,
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Undefined index and constant question

Post by requinix »

Code: Select all

$feet = $_GET["feet"];
if ($_GET[feet] != NULL) {
One of these things is not like the other...
PHP_Wanabe
Forum Newbie
Posts: 7
Joined: Tue Apr 13, 2010 10:55 pm

Re: Undefined index and constant question

Post by PHP_Wanabe »

tasairis wrote:One of these things is not like the other...
adding the double quotes on the second statement:

Code: Select all

$feet = $_GET["feet"];
if ($_GET["feet"] != NULL) {
gives me:

Code: Select all

Notice: Undefined index: feet in C:\xchido\10-8.php on line 9

Notice: Undefined index: feet in C:\xchido\10-8.php on line 10
adding single quotes to the second statement:

Code: Select all

$feet  = $_GET['feet'];
if ($_GET['feet'] != NULL) {
gives me:

Code: Select all

Notice: Undefined index: feet in C:\xchido\10-8.php on line 9

Notice: Undefined index: feet in C:\xchido\10-8.php on line 10
The only difference is in the output. once I used the corresponding single quotations marks, the output after entering a value for feet in the form resulted in a error free display.

Still getting the error message when the form is opened.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Undefined index and constant question

Post by requinix »

Question:

$_GET uses stuff in the URL, right? So what if "feet" isn't in there?
PHP_Wanabe
Forum Newbie
Posts: 7
Joined: Tue Apr 13, 2010 10:55 pm

Re: Undefined index and constant question

Post by PHP_Wanabe »

Thank you for your help. it got me started in the right path. ti took me awhile to figure out the php manual and had to ask for help on another forum but I think it was worth it.

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

Thanks,


This is the final code. Maybe somebody else can use it and learn from this example.


Code: Select all

<html>
<head>
<title>Feet to Meters Comversion Tool</title>
</head>
<body>
<?php
// using error reporting to clean code  once I get everything working it will be taken out
error_reporting(E_ALL);
//check to see if the form has been submitted
if (isset($_GET['feet'])) {
$feet = $_GET['feet'];
echo "<strong>$feet </strong> feet converts to <strong>";
echo $feet * 0.3048;
echo "</strong> meters.<br />";
}
else {
$feet = '';
}
?>
<form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="GET">
<label>Feet:
<input type="text" name="feet" value="<?php echo $feet; ?>" />
</label>
<input type="submit" value="Convert!" />
</form>
</body>
</html>
PHP_Wanabe
Forum Newbie
Posts: 7
Joined: Tue Apr 13, 2010 10:55 pm

Re: Undefined index and constant question

Post by PHP_Wanabe »

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 :lol: )

The form does what it is supposed to do and it has no other problems.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Undefined index and constant question

Post by requinix »

Warnings are there to warn you. Right now there might not be a problem but if the situation changes - different variables, upgraded PHP, etc - then it could be worse.
If you want to ignore them go ahead. But it's like ignoring a warning light on your car.

And... having what malformed?
Post Reply