Skip to content Skip to sidebar Skip to footer

Assign Selected Option Value From Database

I have a form which contains a dropdown list of countries generated from database. The values are stored in database. There is an option in which user can view or update the values

Solution 1:

<selectid="list"name="list"><optionvalue=""> Please Select </option><?$list = array('1',
                        '2',
                        '3',
                        '4',
                        '5',
                        '6');

        while ($L = array_shift($list)) {
            ?><optionvalue="<?=$L?>"<?if($selected == $L){ echo'selected="selected"'; }?> ><?=$L?></option><?
         }
    ?></select>

you can simple get the selected option with this:

$("#list").val();

try please.

Solution 2:

$selected = $list["country_country_name"];

<tr><td>Country</td><td><selectonchange="getCountry(this.value);"name="country"id="country" ><?phpforeach( $queryas$qry ) { 
    $sel = '';
   if( $qry["country_country_name"] == $selected ) 
    $sel = 'selected="selected"';           

    echo'<option value="'.$qry["country_country_name"].'" '.$sel.'>'.$qry["country_country_name"].'</option>'."\n";
    } ?></select><?phpecho form_error('country'); ?></td></tr>

Solution 3:

When your option tag's value attribute contains the same value as the option's text, the value attribute is not necessary -- so omit it.

Below shows an inline condition statement.

<tr><td>Country</td><td><selectonchange="getCountry(this.value);"name="country"id="country"><?phpforeach ($queryas$row) { 
            echo"<option" ,
                ($row["country_country_name"] == $list['country_country_name'] ? ' selected' : '') ,
                "{$row['country_country_name']}</option>\n";
        }
        ?></select></td></tr>

Post a Comment for "Assign Selected Option Value From Database"