Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populate a Select list AND give a default value

Status
Not open for further replies.

rrsub

MIS
Joined
Oct 23, 2002
Messages
536
Location
US
I have a Select list being populated from a database. I would like to have a default value as one of the options.

What is wrong with this code? $location is the value I want set. $myrow is pretty self explanatory


print &quot;<td><select name='chg_loc'>&quot;;

print &quot;<option value = '$location'></option>&quot;;

$result_loc = mysql_query(&quot;SELECT DISTINCT co_name from hosting_co ORDER by co_name&quot;);

while ($myrow = mysql_fetch_array($result_loc)) {
printf('<option value=&quot;%s&quot;>%s %s', $myrow[&quot;co_name&quot;], $myrow[&quot;co_name&quot;], &quot;\n&quot;);
}

print '</select></td>';

 
I dunno what's wrong with this code. What is it doing that it should not or not doing that it should?

You've mentioned default values with SELECT tags, but nowhere do I see you setting the &quot;selected&quot; attribute of any particular option.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
There are other 'text' entries and another 'select' with a 'submit' and a </form>

What it does now is just populate the 'option's so I have all the choices of 'co_name' in TABLE 'hosting_co' with the default choice being blank.

I would like to have a default value dependent on a variable that meets another condition. (which is one of the 'option' values)
 
and u have to close the option using </option>...

Known is handfull, Unknown is worldfull
 
I just really needed to sleep on this. Still a newbie and I'm sure there is a better way but here's my solution:

print &quot;<td><select name='chg_loc'>&quot;;

print &quot;<option value = '$location'>$location</option>&quot;;

$result_loc = mysql_query(&quot;SELECT DISTINCT co_name FROM hosting_co ORDER by co_name &quot;);

while ($myrow = mysql_fetch_object($result_loc)) {
if ($location == $myrow->co_name){} else {

print &quot;<option value='$myrow->co_name'>$myrow->co_name</option>&quot;;
}
}
print &quot;</select></td>&quot;;

Thanks all
 
That won't work. You want it to include all the other locations in your <SELECT>, don't you?

You want to print out each <OPTION>...</OPTION> for each location. It's just that if the location pulled from the database matches the location entered, you want to output &quot;SELECTED&quot; for that option.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
This does work. This is an editing script.

$location already has a value.

For the SELECT, the value gets POSTed to chg_loc with $location as the first choice just in case the user won't change that field.

The subsequent choices are populated as long as they don't = $location in case the user DOES want to change that field and has al the choices.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top