Page 1 of 1

Problem with Notices

Posted: Mon Aug 20, 2007 6:34 pm
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

Posted: Mon Aug 20, 2007 6:43 pm
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']

Posted: Mon Aug 20, 2007 6:54 pm
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.

Posted: Mon Aug 20, 2007 6:57 pm
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.

Posted: Mon Aug 20, 2007 7:01 pm
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.

Posted: Mon Aug 20, 2007 7:03 pm
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.

Posted: Mon Aug 20, 2007 7:29 pm
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.

Posted: Mon Aug 20, 2007 7:42 pm
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?

Posted: Mon Aug 20, 2007 8:16 pm
by reson
Yes, i needed to change the "key" to "search", stupid mistake. It works now, thanks for all the help!!