Suggestion for resolving undefined index

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
techlinks
Forum Newbie
Posts: 18
Joined: Sun Dec 29, 2002 4:00 am

Suggestion for resolving undefined index

Post 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;

?>
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post by lostboy »

try

Code: Select all

if (isset($_GET['cobrand'])){
   $cobrand = $_GET['cobrand']; 
}
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
techlinks
Forum Newbie
Posts: 18
Joined: Sun Dec 29, 2002 4:00 am

Post by techlinks »

You guys have been great help. I understand the contstraints now. Thanks
techlinks
Forum Newbie
Posts: 18
Joined: Sun Dec 29, 2002 4:00 am

Post by techlinks »

To lostboy or others...how did you do accomplish the color formatting on the code with your response? Just curious.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
Post Reply