Tony Crouch
Tony Crouch

Reputation: 39

default select value from mysql query in dropdown box

You guys have been patient with me and very helpful so far, so I'll try another one. I have looked around on here and tried many different options of select="selected" and I just can't get the code right or something, a simple fix I'm sure for you guys. What I'm trying to do is have a drop down list that is populated from a table of countries, default to the United States. Here is the code:

<select name="cnt" id="">
<option value="">Select</option>
<?php
$rescnt=mysql_query("select * from `country` ORDER by cnt_name ASC");
while($rowcnt=mysql_fetch_array($rescnt)) {
?>
<option value="<?php echo $rowcnt['cnt_id']; ?>"><?php echo $rowcnt['cnt_name']; ?></option>
<?php } ?>
</select>

Upvotes: 0

Views: 1970

Answers (1)

kainaw
kainaw

Reputation: 4334

Assuming the cnt_id for the United States is 'US', the option line should be:

<option value='<?php echo $rowcnt['cnt_id']; ?>'<?php if($rowcnt['cnt_id']=='US') echo ' selected'; ?>><?php echo...

Upvotes: 1

Related Questions