Jump to content

Add a default value to my select Options


AikenDrum

Recommended Posts

Hi there,

 

greetings.

 

I have a bit of working code that I need to modify, and just know that someone here can help.

 

I simply need to add a default value to the option list - it should appear as a default in the list (i.e. the list is given alphabetically, but this has to appear at the top) The default value in question is "Germany" - (I know - never mind)

 

$landMenu = '<select style="width: 183px" id="ZS_Land" name="ZS_Land" onchange="addCheckBox()">\n';
foreach ($records as $record){
		$land =$record->getField('english');
		if(empty($land)){
		unset($land);
		continue; 
		}
	    $landMenu .= "<option value='$land'>$land</option>\n";
}
$landMenu .= '</select><br clear ="all"/>';
}
echo $landMenu

 

Any help greatly appreciated - and when you visit Scotland, rewarded with a fish supper

 

Sincere thanks,

 

Aiken D

Hi AikenDrum,

 

Something like the below should work:

 

$landMenu .= '<option value="'.$land.'" '.($land == "Germany" ? 'selected="selected"' :'').'
>'.$land.'</option>';

 

Just inserted an ternary operator statement to check the $land variable and output "selected" if it's found.

 

Hope this helps.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.