0

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>
2
  • you haven't got the selected attribute printed anywhere in the example above. Commented Mar 9, 2013 at 15:07
  • You are using an obsolete database API and should use a modern replacement. Commented Mar 10, 2013 at 21:46

1 Answer 1

1

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...
Sign up to request clarification or add additional context in comments.

2 Comments

I tried this but I get this error: Parse error: syntax error, unexpected T_VARIABLE, expecting '(' in /home/content/44/10329144/html/admin/additem.php on line 258
I added parenthesis around the if clause. I assume that is what caused the error. Without knowing what is on line 258, it is hard to tell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.