MySQL Forums
Forum List  »  PHP

Re: undefined index error
Posted by: Scott Miller
Date: November 14, 2006 12:09AM

Chandran:

This is a pure PHP issue, but I have the answer...

The $_GET[ ] variables in PHP are only used to grab stuff off the URL. Typically you'd use this when passing data in a web form or when passing information to a generator page that then customizes itself according to the data.

For a web form on your form page you'd do something like...
<form method="get" action="process_form.php">
  <p>What is your name: <input type="text" size="30" maxlength="30" name="name" /></p>
  <p>What is your friend's name: <input type="text" size="30" maxlength="30" name="friend" /></p>
  <button type="submit" name="whatNext" value="go">Go!</button>
</form>
The URL for the process_form.php page would be something like...
http://www.domain.com/process_form.php?name="Bozo Smith"&friend="Barney Jones"
On the process_form.php page is where you'd use $_GET[ ], just like you have it in your example.


In another example we could generate a web page from a database of... let's say forum board users. We could have a unique DB index which is a simple auto-incrementing number and we could have a page that generates a list of forum members (sorted in various ways). One of the (many) links on that page could be coded...
<a href="user_profile.php?userID=42">Bozo Smith</a>
On the user_profile.php page we'd have something like...
<?php
  $userID = $_GET['userID'];
?>
Which would set $userID to 42.

Because some people like to futz with their URLs, testing is a good idea. The simplest way to break your code is to leave off the variable...
<?php
  if (isset ($_GET['userID'])) {
    $userID = $_GET['userID'];
    //more code to test the validity of $userID
  } else {
    $userID = 1    //set some default value
  }
?>

So the short answer is, $_GET[ ] is empty because you fail to set it.

Scotty

Options: ReplyQuote


Subject
Written By
Posted
November 13, 2006 04:38AM
November 13, 2006 08:21PM
November 13, 2006 10:27PM
Re: undefined index error
November 14, 2006 12:09AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.