hello i have a dropdown which is working of sorts, however instead selecting the empyt selected value 'select an address' it always shows the last entry in the dropdown instead of the empty selected value. i intially had help with code from another source but seem to have lost my way with it. where am i going wrong? many thanks
PS. sorry about the layout, but this is how it was layed out after pressing the code button.
> $address_result = mysql_query("SELECT
> * FROM company_com ORDER BY address1_com ASC"); $dropdown =
> "<select name='address'>"; $dropdown
> .= "<option SELECTED value=\"\">Select
> an Address</option>";
while($row = mysql_fetch_assoc($address_result)) {
$address = $row["address1_com"].$row["address2_com"].$row["address3_com"];
$dropdown .= "<option SELECTED value='".$row['address']."'>$address</option>";
}
$dropdown .= "</select>";
-
Remove the
SELECTEDfrom:$dropdown .= "<option SELECTED .....From Wrikken -
This loop sets the
selectedattribute of all theoptiontags. View your source to verify. The browser defaults to show the last selected. Which is why the last one in the loop is selected.You need to do a check in PHP to test if the current output is the selected address and only then set the
selectedattribute. If none get this attribute, the browser will default to the first option - in your case the default.You should also output this attribute as
selected="selected"for proper HTML standards.Wrikken : `selected` on its own is perfectly valid `HTML 4.01 Strict`, only `XHTML` requires `selected="selected"`Jason McCreary : Good clarification. Been a long time since I wrote HTML 4. I still advise `selected="selected"` for *best practice*.Wrikken : Best practice is to know the HTML you're working with, self-closing tags for instance are required in XHTML, but illegal in HTML...Jason McCreary : Fair, but we're not talking about minimized tags. `selected="selected"` is not invalid for HTML and it has the benefit of being future proof. Just being devils advocate ;)From Jason McCreary -
<? $address_result = mysql_query("SELECT * FROM company_com ORDER BY address1_com ASC"); $dropdown ="<select name='address'>"; $dropdown.= "<option SELECTED value=\"\">Select an Address</option>"; while($row = mysql_fetch_assoc($address_result)) { $address = $row["address1_com"].$row["address2_com"].$row["address3_com"]; $dropdown .= "<option value='".$row['address']."'>$address</option>"; } $dropdown .= "</select>"; ?>Done
Mr.Putersmit : thanks ollie. simple eh :-) one las thing. because some of the addreess's are quite long is there way to restrict the size of the dropdown in terms of width? thanksMr.Putersmit : ok. did it with css. thanksFrom Ollie
0 comments:
Post a Comment