In edit profile form I want to display selected value of Country dropdown list as value saved in db and user can change that also.
A:
you need to set the selected attribute:
<select name="country">
<option<?= $country == "USA" ? ' selected="selected"' : ''?>>USA</option>
<option<?= $country == "Kanada" ? ' selected="selected"' : ''?>>Kanada</option>
<option<?= $country == "Mexico" ? ' selected="selected"' : ''?>>Mexico</option>
</select>
UPDATE
<? $countries = array(/* Array of countries */); ?>
<select name="country">
<? foreach($countries as $c): ?>
<option<?= $c == $country ? ' selected="selected"' : '' ?>><?= $c ?></option>
<? endforeach; ?>
</select>
jigfox
2010-05-27 07:37:12
@aSeptic: That is exactly the way to do this. For 30 ore more options you have an array with these options and can loop through it! The first example was just meant to be short.
jigfox
2010-05-27 07:45:34
the second one is just fine! ;-)
aSeptik
2010-05-27 07:52:19
I made my first example without array, because 'ramukaka' didn't post any code, and I didn't want to make to much assumptions how it might look like
jigfox
2010-05-27 08:01:07
A:
<select id="user" name="user[sex]" >
<option value="male" <?php $sex == "male" ? "selected" : '' ?>>Male</option>
<option value="female" <?php $sex == "female" ? "selected" : '' ?>>Female</option>
</select>
Salil
2010-05-27 07:39:20
+1
A:
echo "<select name='cmbCountry'>";
while($country = mysql_fetch_array($countries)){
echo "<option value='".$country["id"]."'".($country["id"] == $profile["idCountry"]) ? " selected='selected'" : "".">".$country["name"]."</option>"
}
echo "</select>";
Jeaffrey Gilbert
2010-05-27 07:43:41
A:
slightly advanced version of Jens' answer - makes it easier to add new options:
<?php $countries = array('USA', 'Kanada', 'Mexico'); ?>
<select name="country">
<?php foreach ($countries as $c): ?>
<option<?php $country == $c ? ' selected="selected"' : ''; ?>>$c</option>
<?php endforeach; ?>
</select>
HorusKol
2010-05-27 07:44:00