-2

I want a default value and get rid off the notice. Below is my code

<?php
$color = $_POST["color"]; //initialize variable
if (!isset($color)){    // if nothing selected set default to Blue
    $color = 'Blue';
}
echo $color;    
?>

So when I run the code and nothing is selected and the blue value (as it is set for default) does show up but the notice message also shows up. I want to get rid of the notice message completely. I have tried array_key_exists as well. Still can not get rid of the notice.

below is the notice I'm getting. Notice: Undefined index: color in C:\xampp\htdocs\test3\Assignment_1_Q2.php on line 17

Thank you in advance.

Shadid
  • 4,133
  • 9
  • 30
  • 53

1 Answers1

0

Try this, First check the value with isset function.

<?php
$color = ""; //initialize variable
if(isset($_POST["color"]))
{
    $color = $_POST["color"];
}
else
{
    // if nothing selected set default to Blue
    $color = 'Blue';
}
echo $color;    
?>
Ankur Tiwari
  • 2,762
  • 2
  • 23
  • 40