Forums

This topic is locked

How to pre-select a countryname?

Posted 08 Aug 2005 12:58:15
1
has voted
08 Aug 2005 12:58:15 lee lee posted:
Hi Everybody,

Does anyone knows how to pre-select a Country-name from a menu? For example, if I am from Singapore, I want to have a menu that shows Singapore being selected before I could even select manually. How is that possible?

You can go to www.dell.com website and see that the menu on top-left corner show United States being selected.

I am using a table having countryID and a countryName. Well I can sort ascending or descending manner according to countryID or countryName in Dreamweaver recordset, but when i bind it to a menu, it shows me according to what my SQL dictates. That is sort according to countryID or countryName.

What gives?

Thanks for reading!

Replies

Replied 08 Aug 2005 15:10:45
08 Aug 2005 15:10:45 Wayne Hultum replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
Hi Everybody,

Does anyone knows how to pre-select a Country-name from a menu? For example, if I am from Singapore, I want to have a menu that shows Singapore being selected before I could even select manually. How is that possible?

You can go to www.dell.com website and see that the menu on top-left corner show United States being selected.<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

Just put selected after the value, eg. value="Singapore" <b>selected</b>
Replied 08 Aug 2005 18:20:38
08 Aug 2005 18:20:38 myke black replied:
If your script writes all the country options to the HTML and you do not have the ability to add the selected attribute inline, what you could do is add a bit of script to the end of the page like this:
<pre id=code><font face=courier size=2 id=code>

&lt;script&gt;
var strCountry = "Singapore"
for (a=0;a&lt;document.form1.country.length;a ##){
if (document.form1.country[a].text == strCountry) {
document.form1.country.selectedIndex = a;
}
}
&lt;/script&gt;
</font id=code></pre id=code>
(swap the # for plus sign - for some reason the plus sign does not display in this forum(??)


If you want to auto select the country based on what your own country is, you need to use an ASP add on that can estimate your country based on your IP address, something like IP2location component. Unfortunately, I've not found a free version thats as good as the ones you pay for, but I'm sure there are some good ones out there somewhere
Replied 09 Aug 2005 19:49:14
09 Aug 2005 19:49:14 lee lee replied:
Thanks for the prompt reply.

But I am storing my countries in a field name called countryName with countryID as the unique key. So I do a SQL query and all the countries are sorted in an ascending order. Then I databind my SQL query to the menu. The problem is I want to have Singapore being selected whilst all the countries are there in the menu as well. What SQL query do i need to execute so that Singapore will be pre-selected?

Though Wayne has suggested to me to use the value="Singapore" selected method, it only workd for static data.

Thanks for reading!
Replied 10 Aug 2005 19:21:16
10 Aug 2005 19:21:16 myke black replied:
You cant preselect the required country using the SQL query, however, you can rewrite your code to write the contents of the recordset so that instead of using a bind method, you use a while loop , something like this:

&lt;select name="country"&gt;
&lt;option value=""&gt;Select your country&lt;/option&gt;
&lt;%
set conn = server.createObject("adodb.connection"
conn.open([CONNECTION STRING HERE])
SQL = "select * from countries"
set rs = server.createObject("adodb.recordset"
if not rs.eof then
while not rs.eof
response.write "&lt;option value=""" & rs.fields("countryID" & """
if rs.fields("countryName" = "Singapore" then response.write " selected"
response.write "&gt;" & rs.fields("countryName" & "&lt;/option&gt;" & vbCrLf
rs.movenext
wend
end if
set rs = nothing
conn.close
set conn = nothing
%&gt;
&lt;/select&gt;
Replied 12 Aug 2005 03:53:35
12 Aug 2005 03:53:35 lee lee replied:
Hi myke,

Thanks for your prompt reply. I will dissect it and try it out!

Cheerios!

Reply to this topic