Problem with Notices

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
reson
Forum Newbie
Posts: 7
Joined: Sat Jun 30, 2007 8:06 pm

Problem with Notices

Post by reson »

Hi, I'm a newbie, just trying to learn php. I'm getting some Notices that won't go away, I've looked at other posts here but haven't been able to figure it out myself.

I'm getting the following Notices in when I run my code.

Notice: Undefined index: search in C:\XXX\form_test.php on line 8

Notice: Undefined index: php_self in C:\XXX\form_test.php on line 9

code:

Code: Select all

<html>
<head>
 <title>Form test</title>
</head>
<body>
<?php

$search = $_GET["search"];
$self = $_SERVER["php_self"];
if ($search !=null)
{
('

<form action="'.$_SERVER["PHP_SELF"].'" method="get">
  <label>Search: <input type="text" name="search" /></label>
  <input type="submit" value="Go!" />
 	</form>
	');
}
?>
</body>
</html>
Any help you have would be appreciated (but please don't tell me to turn my notices off, I want to know why i'm getting them!)

Thanks
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

You have to do some checks before getting a value from $_GET array.

Code: Select all

if (!empty($_GET['key']))
{
      $key = $_GET['key'];
}
else
{
      $key = null;
}
And it is $_SERVER['PHP_SELF']
There are 10 types of people in this world, those who understand binary and those who don't
reson
Forum Newbie
Posts: 7
Joined: Sat Jun 30, 2007 8:06 pm

Post by reson »

Where do the $_GET checks go in my code? Right before the $search = ...? I'm still getting the notice for that one.

"And it is $_SERVER['PHP_SELF']"

Thanks, this worked for the second notice.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

reson wrote:Where do the $_GET checks go in my code? Right before the $search = ...? I'm still getting the notice for that one.
What have you tried? Post code, please.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Brief tutorial on arrays...

array_name[array_index] is the format for a single, first dimension array member. When you reference an index in an array that index has to have been defined in that array or you will get the errors you are getting. Here is an example...

Code: Select all

<?php
$cars = array(
  'Honda' => 'CRV',
  'Toyota' => 'Highlander'
);
?>
The above array now has two members that are referenced by indexes 'Honda' and 'Toyota'. If you attempt to call the array index 'Chrysler' what do you suppose will happen? Exactly. PHP will get mad at you for asking for something it does not have nor knows anything about.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

And a quick note... don't use $_SERVER['PHP_SELF']. There are many posts around here as to why, but don't use it. Use something like basename(__FILE__) or something similar, but don't use PHP_SELF.
reson
Forum Newbie
Posts: 7
Joined: Sat Jun 30, 2007 8:06 pm

Post by reson »

I tried this:

Code: Select all

<?php

if (!empty($_GET['key']))
{
      $key = $_GET['key'];
}
else
{
      $key = null;
} 

$search = $_GET["search"];
$self = $_SERVER['PHP_SELF'];
if ($search !=null)
...
But I get Notice: Undefined index: search in C:\XXX\form_test.php on line 17, which is the "$search = $_GET["search"];" line.


And Everah, thanks for letting me know about $_SERVER['PHP_SELF'], I'll look it up.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

The notice you are getting means there is no 'search' index for the $_GET array. Are you passing ?search=something to your script? Also, you are checking for the array index called 'key' but you are trying to use the array index 'search'? Is that what you are intending to do?
reson
Forum Newbie
Posts: 7
Joined: Sat Jun 30, 2007 8:06 pm

Post by reson »

Yes, i needed to change the "key" to "search", stupid mistake. It works now, thanks for all the help!!
Post Reply