49

This is going to sound really stupid, but I cannot figure out why I am getting this error.

I have created a selection box, named "query_age" in my html form:

<form method="get" action="user_list.php">
<select name="query_age">
  <option value="">Doesn't matter</option>
  <option value="between 18 and 30">18 - 30</option>
  <option value="between 31 and 40">31 - 40</option>
  <option value="between 41 and 50">41 - 50</option>
  <option value="between 51 and 60">51 - 60</option>
  <option value="between 61 and 70">61 - 70</option>
  <option value="between 71 and 80">71 - 80</option>
  <option value="between 81 and 90">81 - 90</option>
  <option value="> 90">Older than 90</option>
</select>

In the corresponding php form, I have:

$query_age = $_GET['query_age'];

When I run the page, I get this error:

Notice: Undefined index: query_age in index.php on line 19

I don't understand why this is happening, and I'd love to know how to make it go away.

Vic
  • 108
  • 9
  • 2
    What does `print_r($_GET)` show? Are other form values there? Why don't you use POST? – Felix Kling Jan 30 '11 at 12:03
  • In which way does the _corresponding php form_ correspond to the HTML form? Does it generate the HTML form? Does it process the values that are submitted via the HTML form? Does it just have the same base name with a different extension? Please enlighten us. – Oswald Jan 30 '11 at 12:06
  • 2
    You know that you have to process the form in `user_list.php` as you have specified this in the form's action? It would help if you could post relevant code of `index.php` and explain the general "flow" of your application. – Felix Kling Jan 30 '11 at 12:30
  • 1
    You should also replace ">90" by ">90". – Aif Jan 30 '11 at 12:34
  • Something else you want to check for is the html itself. Make sure you are using the right form attribute names you are expecting in your php script. – JohnMerlino Jul 26 '14 at 21:29

4 Answers4

100

I don't see php file, but that could be that -
replace in your php file:

$query_age = $_GET['query_age'];

with:

$query_age = (isset($_GET['query_age']) ? $_GET['query_age'] : null);

Most probably, at first time you running your script without ?query_age=[something] and $_GET has no key like query_age.

Brian Moeskau
  • 20,103
  • 8
  • 71
  • 73
Radek Benkel
  • 8,278
  • 3
  • 32
  • 41
  • 1
    This will just suppress the notice, not fix the cause. – mario Jan 30 '11 at 12:09
  • 8
    @mario I explained cause below code. Author wrote: "When I run the page, I get this error: Notice: Undefined index: query_age in .../index.php on line 19" so I think, that my answer is ok to fix that. – Radek Benkel Jan 30 '11 at 12:13
  • 1
    Fixing the notice != fixing the reason. But I'm not disputing that it answers the OPs "make it go away" question part. Yet, the actual problem lies elsewhere. – mario Jan 30 '11 at 12:19
  • 2
    Good idea if the script that processes the form is the same that produces the form. But in this case it seems to be two different scripts. The error is thrown in `index.php` while the form's action is `user_list.php`. – Felix Kling Jan 30 '11 at 12:28
  • $query_age = (isset($_GET['query_age']) ?: null) – b00sted 'snail' Apr 20 '17 at 12:15
9

The checking of the presence of the member before assigning it is, in my opinion, quite ugly.

Kohana has a useful function to make selecting parameters simple.

You can make your own like so...

function arrayGet($array, $key, $default = NULL)
{
    return isset($array[$key]) ? $array[$key] : $default;
}

And then do something like...

$page = arrayGet($_GET, 'p', 1);
alex
  • 479,566
  • 201
  • 878
  • 984
4

The first time you run the page, the query_age index doesn't exist because it hasn't been sent over from the form.

When you submit the form it will then exist, and it won't complain about it.

#so change
$_GET['query_age'];
#to:
(!empty($_GET['query_age']) ? $_GET['query_age'] : null);
Jason
  • 15,064
  • 15
  • 65
  • 105
  • So long as the presence of the GET param is never important to your application (it rarely is). – alex Jan 30 '11 at 12:16
1

if you use isset like the answer posted already by singles, just make sure there is a bracket at the end like so:
$query_age = (isset($_GET['query_age']) ? $_GET['query_age'] : null);

NSDestr0yer
  • 1,419
  • 16
  • 20