I coded a form script on my system, it runs IIS. When I moved it to test, that server is running Apache, and now I'm getting all sorts of 'Undefined index' errors.

Notice: Undefined index: user_name in c:\program files\apache group\apache\htdocs\poker\leagueman.php on line 52 

I get the erros when I first load the page. I've looked at the variables coming into the page using print_r($GLOBALS), and it shows an empty array when the page first loads. That's fine. However, I'm referencing the variables inside my form. The form looks like this:

echo '<INPUT TYPE="TEXT" NAME="user_name" VALUE="' . $_POST['user_name'] . '" SIZE="20" MAXLENGTH="20">';

I've found a work around using isset.

if (isset($_POST['user_name'])) {
	$user_name = $_POST['user_name'];
}
else {
	$user_name = NULL;
}

If I use the above code to handle it, now I have to go in and change every form element from $POST['user_name'] to $user_name, and also change any variables being passed into a function from $POST['user_name'] to $user_name.

I really don't want to have to rewrite my code for every form variable. Is there an easier way to get around this?

I know about using '@' in the front to suppress the error messages, but is that really the standard?

What is the best way to handle these types of messages?

Thanks!
-J

    The best method would be to do a search and replace (Control+H or Control+F is the common keystroke) and do as you said; replace the $_POST[] variable with the newly created variable.

    This is the only SAFE, RELIABLE coding method. Using error surpressors, or disabling the E_NOTICE error code is simply unwise and generally not a good coding practice.

    Get into the habit of properly declaring variables now, and you won't have to break that habit in the future. What better way can you think of to imprint this in your memory than to painstakingly comb your script and replace the offending $_POST[] references? 😉

      Originally posted by bradgrafelman
      What better way can you think of to imprint this in your memory than to painstakingly comb your script and replace the offending $_POST[] references? 😉

      Good answer. 5 points to Griffyndor house.

      -J

        Write a Reply...