Perad Posted November 22, 2006 Share Posted November 22, 2006 I have this form[code]<?First Map: <select name="map1" value="'.$m1.'"> <option> </option> <option>Brecourt</option> <option>Burgundy</option> <option>Caen</option> <option>Carentan</option> <option>St.Mere Eglise</option> <option>El Alamein</option> <option>Moscow</option> <option>Beltot</option> <option>Leningrad</option> <option>Matmata</option> <option>Rostov</option> <option>Stalingrad</option> <option>Toujane</option> <option>Wallendar</option> <option>Villers-Bocage</option> </select><br />?>[/code]$m1 is a variable which has already been declared. In this case it is Toujane. How can i make the dropdown default to this value?EDIT: The $m1 is in '. .' because the whole form is wrapped in an echo tag Link to comment https://forums.phpfreaks.com/topic/28087-assigning-a-drop-down-box-a-php-variable/ Share on other sites More sharing options...
JasonLewis Posted November 22, 2006 Share Posted November 22, 2006 firstly, you do not set a value to select tags, you must set it to the option tag. and i would make an array of all the places then do a for statement. oh and why is your html wrapped in <? and ?>?[code=php:0]<?phpecho "First Map: <select name='map1'>";$m1 = "Toujane"; //default place$places = array('Brecourt','Burgundy','Caen','Carentan','St.Mere Eglise','El Alamein','Moscow','Beltot','Leningrad','Matmata','Rostov','Stalingrad','Toujane','Wallendar','Villers-Bocage');for($i = 0; $i < count($places); $i++){if($places[$i] == $m1){$extra = "selected";}else{$extra = "";}echo "<option value='".$places[$i]."'>".$places[$i]."</option>";}echo "</select>";[/code] Link to comment https://forums.phpfreaks.com/topic/28087-assigning-a-drop-down-box-a-php-variable/#findComment-128484 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.