Jump to content

Assigning a drop down box a php variable?


Perad

Recommended Posts

I have this form

[code]<?
First Map:&nbsp;<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

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]
<?php
echo "First Map:&nbsp;<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]

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.