Jump to content

default a value in drop down


jjmusicpro

Recommended Posts

i was pulling a value from mysql, and i wanted to put that value into the drop down.

 

the drop down has 3 values seattle, phoenix, and new york.

 

the value from the drop down is always going to match one pulled from db..

 

so...

 

if the value pulled from the db is say seattle, i wanted seattle to be selected in the drop down menu, but they can change it if they want, i just want it to be selected when they go to the page, with the values that match it form the db

Link to comment
https://forums.phpfreaks.com/topic/107495-default-a-value-in-drop-down/
Share on other sites

how do i make it so it wont write out that value again... so

i pull "seattle" form the guys profile, and i make a form that i wanted to edit his info.

 

so the form will have a select drop down box, with 3 values seattle, phoenix, and new york.

 

instead of the drop down box starting off with phoenix or some other place, i wanted it to show the value selected as the one in the db.

i tried setting an option value as you said but it didnt work

 

<select name=\"payscale\">

<option selected=\"$payscale\"></option>

<option value=\"seattle\">seattke</option>

<option value=\"phoenix\">phoenix</option>

<option value=\"new york\">new york</option>

</select>

No no.

 

<select name=\"payscale\">
<option selected=\"selected\"></option>
<option value=\"seattle\">seattke</option>
<option value=\"phoenix\">phoenix</option>
<option value=\"new york\">new york</option>
</select>

 

It must be selected="selected"

Try:

 

<?php
$cities = array('seattle','pheonix','new york');//array of cities
//assuming $payscale is the variable containing the state name:
echo '<select name="payscale">';
foreach($cities as $v){
    $selected = '';
    if($v == $payscale){
        $selected = ' selected="selected" ';
    }
    echo '<option value="'.$v.'"'.$selected,'>'.$v.'</option>';
}
echo '</select>';
?>

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.