Page 1 of 1

Suggestion for resolving undefined index

Posted: Wed May 26, 2004 3:48 pm
by techlinks
May I have someone's suggestion as to how to prevent PHP from reporting the following message:

Notice: Undefined index: cobrand in c:\inetpub\wwwroot\test.php on line 5
cobrand is empty

...when the code below is executed?

Thanks everyone.

Lester

///////////////////////////////////////////

<?php

$myform = $_SERVER['PHP_SELF'];
$formsent = $_GET['formsent'];
$cobrand = $_GET['cobrand'];

$theform = "
<form action=\"$myform\" method=\"get\">
<select name=\"cobrand\">
<option>mybrand1</option>
<option>mybrand2</option>
</select>
<input type=\"submit\">
<input type=\"hidden\" name=\"formsent\" value=\"true\">
</form>
";

///////////////////////////////////////////

if(isset($formsent))
{
echo "cobrand is $cobrand";
echo "formsent is $formsent";
}
else
{
echo "cobrand is empty";
}

print $theform;

?>

Posted: Wed May 26, 2004 3:55 pm
by lostboy
try

Code: Select all

if (isset($_GET['cobrand'])){
   $cobrand = $_GET['cobrand']; 
}

Posted: Wed May 26, 2004 4:05 pm
by kettle_drum
You just get the warning as there is no 'cobrand' defined in the get. If you add it to the url, then you wont see the error any more, but you can never be sure what data will be sent, so you should do as lostboy said.

Posted: Thu May 27, 2004 11:33 am
by techlinks
You guys have been great help. I understand the contstraints now. Thanks

Posted: Thu May 27, 2004 11:34 am
by techlinks
To lostboy or others...how did you do accomplish the color formatting on the code with your response? Just curious.

Posted: Thu May 27, 2004 11:53 am
by JayBird
techlinks wrote:To lostboy or others...how did you do accomplish the color formatting on the code with your response? Just curious.
read this - http://www.devnetwork.net/forums/viewtopic.php?t=21171