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.

5
  • 2
    What does print_r($_GET) show? Are other form values there? Why don't you use POST? Commented Jan 30, 2011 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. Commented Jan 30, 2011 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. Commented Jan 30, 2011 at 12:30
  • 1
    You should also replace ">90" by "&gt;90". Commented Jan 30, 2011 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. Commented Jul 26, 2014 at 21:29

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

This will just suppress the notice, not fix the cause.
@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.
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.
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.
$query_age = (isset($_GET['query_age']) ?: null)
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);

5 Comments

Nice one, only I suggest to use Anonymous functions to be in control of scope and to avoid errors like <Cannot redeclare ... (previously declared...>
Where would an anonymous function fit in this example?
like this $arrayGet = function...
What are the advantages of that?
I listed them above 1) scope 2) redeclaration
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);

1 Comment

So long as the presence of the GET param is never important to your application (it rarely is).
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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.