Skip to content
Advertisement

Select box with first option empty

How can I set the first option in my select box to an empty value?

I’m getting the data from my DB, and I would like to set the option by default as “Please select one option”.

Advertisement

Answer

I found that 'default'=>'Please select' doesn’t work with the HTML5 required attribute. This does work:

$listOfValues = [1 => 'Choice 1'];
Form::select('fieldname',[null=>'Please Select'] + $listOfValues);

If you don’t like modern PHP syntax,

$listOfValues = array(1 => 'Choice 1');
$listOfValues[null] = 'Please Select';
Form::select('fieldname', $listOfValues);

But the point is to have a label for the null value.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement