agodil
asked on
setting a default value for a form field in asp
hello ! our web site requires customers to create an account before they begin shopping and to create an account they have to enter their address . On that form i would like to set the default value to USA. the code for that page is as follows
<%If Not rsCountries.eof then%>
<select class=content name="COUNTRY" tabindex="14" size="1">
<%If Request.form("country") <> "" Then Do While not rsCountries.Eof
If ucase(Request.form("countr y")) = ucase(rsCountries("COUNTRY _NAME")) Then%>
<option value="<%=rsCountries("COU NTRY_NAME" )%>" selected><%=rsCountries("C OUNTRY_NAM E")%></opt ion>
<%Else%>
<option value="<%=rsCountries("COU NTRY_NAME" )%>"><%=rs Countries( "COUNTRY_N AME")%></o ption>
<%End If
rsCountries.MoveNext
Loop
Else
Do While not rsCountries.Eof%>
<option value="<%=rsCountries("COU NTRY_NAME" )%>"><%=rs Countries( "COUNTRY_N AME")%></o ption>
<%rsCountries.MoveNextLoop
End If%>
</select>
<%End If%>
</td>
</tr>
<tr>
Please help !
<%If Not rsCountries.eof then%>
<select class=content name="COUNTRY" tabindex="14" size="1">
<%If Request.form("country") <> "" Then Do While not rsCountries.Eof
If ucase(Request.form("countr
<option value="<%=rsCountries("COU
<%Else%>
<option value="<%=rsCountries("COU
<%End If
rsCountries.MoveNext
Loop
Else
Do While not rsCountries.Eof%>
<option value="<%=rsCountries("COU
<%rsCountries.MoveNextLoop
End If%>
</select>
<%End If%>
</td>
</tr>
<tr>
Please help !
ASKER
hey mattfairw,
Thanks you for the suggestion. Unfortunately i still cannot get it to work. This is what i did
it used to be this
Do While not rsCountries.Eof%>
<option value="<%=rsCountries("COU NTRY_NAME" )%>"><%=rs Countries( "COUNTRY_N AME")%></o ption>
<%rsCountries.MoveNextLoop
End If%>
so i changed it to
Do While not rsCountries.Eof%>
<option
value="<%=rsCountries("COU NTRY_NAME" )%>"
<%If strComp(rsCountries("COUNT RY_NAME"), "USA",1) = 0 Then
Response.Write " SELECTED"
End If%>><%=rsCountries("COUNT RY_NAME")% ></option>
</select>
is that right ?? i get an internal server error when i try to load the registration page !
Thanks in advance !
Thanks you for the suggestion. Unfortunately i still cannot get it to work. This is what i did
it used to be this
Do While not rsCountries.Eof%>
<option value="<%=rsCountries("COU
<%rsCountries.MoveNextLoop
End If%>
so i changed it to
Do While not rsCountries.Eof%>
<option
value="<%=rsCountries("COU
<%If strComp(rsCountries("COUNT
Response.Write " SELECTED"
End If%>><%=rsCountries("COUNT
</select>
is that right ?? i get an internal server error when i try to load the registration page !
Thanks in advance !
Do While not rsCountries.Eof%>
<option
value="<%=rsCountries("COU NTRY_NAME" )%>"
<%If rsCountries("COUNTRY_NAME" ) == "USA" Then
Response.Write " SELECTED"
End If%>><%=rsCountries("COUNT RY_NAME")% ></option>
</select>
This ASSUMES that your 'name' in the recordset for United States of America is "USA" and not "United States" or "United States of America" if it is something other than "USA" then change that in the code above to whatever you have in your database for USA.
Cheers,
<option
value="<%=rsCountries("COU
<%If rsCountries("COUNTRY_NAME"
Response.Write " SELECTED"
End If%>><%=rsCountries("COUNT
</select>
This ASSUMES that your 'name' in the recordset for United States of America is "USA" and not "United States" or "United States of America" if it is something other than "USA" then change that in the code above to whatever you have in your database for USA.
Cheers,
First of all, in your last code snippet, I see a "Do While", but I don't see an ending "Loop" statement. I.e., something like:
Do While not rsCountries.Eof
%>
<option value="<%=rsCountries("COU NTRY_NAME" )%>"
<%
If strComp(rsCountries("COUNT RY_NAME"), "USA",1) = 0 Then
Response.Write " SELECTED"
End If
%>><%=rsCountries("COUNTRY _NAME")%>< /option>
<%
Loop
%>
</select>
Do While not rsCountries.Eof
%>
<option value="<%=rsCountries("COU
<%
If strComp(rsCountries("COUNT
Response.Write " SELECTED"
End If
%>><%=rsCountries("COUNTRY
<%
Loop
%>
</select>
You could also, to be more explicit (just in case it makes any difference), replace the above-suggested
<%If rsCountries("COUNTRY_NAME" ) == "USA" Then
with
<% If CStr(rsCountries.Fields("C OUNTRY_NAM E").Value) = "USA" Then
Note that, if you're using VBScript, there should be one equals sign; if you're using JScript, there should be two. :-)
<%If rsCountries("COUNTRY_NAME"
with
<% If CStr(rsCountries.Fields("C
Note that, if you're using VBScript, there should be one equals sign; if you're using JScript, there should be two. :-)
And, as agodil and darksinclair both pointed out, make sure that "USA" is a valid value within your data's COUNTRY_NAME column -- or change "USA" in the above code-examples to match the appropriate value therein.
Hi,
Here is the code. You have to set the selected name in a variable and set it in option tag. This way you will not get error message and good practice.
<%If Not rsCountries.eof then%>
<select class=content name="COUNTRY" tabindex="14" size="1">
<%If Request.form("country") <> "" Then
Do While not rsCountries.Eof
If ucase(Request.form("countr y")) = ucase(rsCountries("COUNTRY _NAME")) Then%>
selected = " selected"
else
selected = ""
end if
<option value="<%=rsCountries("COU NTRY_NAME" )%>" selected><%=rsCountries("C OUNTRY_NAM E")%></opt ion>
<% rsCountries.MoveNext
Loop
Else
Do While not rsCountries.Eof
if Trim(rsCountries("Country_ Name")) = "USA" then
selected = " selected"
else
selected = ""
end if
<option value="<%=rsCountries("COU NTRY_NAME" )%>" selected><%=rsCountries("C OUNTRY_NAM E")%></opt ion>
<% rsCountries.MoveNextLoop
End If%>
</select>
<%End If%>
</td>
</tr>
<tr>
Hope this would be helpful to you.
Here is the code. You have to set the selected name in a variable and set it in option tag. This way you will not get error message and good practice.
<%If Not rsCountries.eof then%>
<select class=content name="COUNTRY" tabindex="14" size="1">
<%If Request.form("country") <> "" Then
Do While not rsCountries.Eof
If ucase(Request.form("countr
selected = " selected"
else
selected = ""
end if
<option value="<%=rsCountries("COU
<% rsCountries.MoveNext
Loop
Else
Do While not rsCountries.Eof
if Trim(rsCountries("Country_
selected = " selected"
else
selected = ""
end if
<option value="<%=rsCountries("COU
<% rsCountries.MoveNextLoop
End If%>
</select>
<%End If%>
</td>
</tr>
<tr>
Hope this would be helpful to you.
ASKER
Hey darksinclair and Thogek, well here's the whole situation. I tried all the posts and they all give me that internal error (500.400 something error). I am using this software called everest that wrote all the asp code for my webpage and im not too sure what's going on in the code that it generated. i did put in UNITED STATES which is one of the options on the web page but im not sure if it's called that on sql db. would this be easier if posted the code for the entire page ?? and also do you know how i might be able to look up what UNITED STATES is in the DB !!!
Thanks
Thanks
ASKER
hey casstd, so i just stuck the code you gave me and did change the USA to UNITED STATES ! it gives me an error sayin Loop with out do !! supwithdat ?
thanks !!
thanks !!
Hi,
The problem is there is two end if statement at the end of the code. It shoule be one. Copy the below code.
<%If Not rsCountries.eof then%>
<select class=content name="COUNTRY" tabindex="14" size="1">
<%If Request.form("country") <> "" Then
Do While not rsCountries.Eof
If ucase(Request.form("countr y")) = ucase(rsCountries("COUNTRY _NAME")) Then%>
selected = " selected"
else
selected = ""
end if
<option value="<%=rsCountries("COU NTRY_NAME" )%>" selected><%=rsCountries("C OUNTRY_NAM E")%></opt ion>
<% rsCountries.MoveNext
Loop
Else
Do While not rsCountries.Eof
if Trim(rsCountries("Country_ Name")) = "UNITED STATES" then
selected = " selected"
else
selected = ""
end if
<option value="<%=rsCountries("COU NTRY_NAME" )%>" selected><%=rsCountries("C OUNTRY_NAM E")%></opt ion>
<%
rsCountries.MoveNext
LOOP
End If
%>
</select>
</td>
</tr>
<tr>
Hope you will not get error message.
The problem is there is two end if statement at the end of the code. It shoule be one. Copy the below code.
<%If Not rsCountries.eof then%>
<select class=content name="COUNTRY" tabindex="14" size="1">
<%If Request.form("country") <> "" Then
Do While not rsCountries.Eof
If ucase(Request.form("countr
selected = " selected"
else
selected = ""
end if
<option value="<%=rsCountries("COU
<% rsCountries.MoveNext
Loop
Else
Do While not rsCountries.Eof
if Trim(rsCountries("Country_
selected = " selected"
else
selected = ""
end if
<option value="<%=rsCountries("COU
<%
rsCountries.MoveNext
LOOP
End If
%>
</select>
</td>
</tr>
<tr>
Hope you will not get error message.
This should work:
<%
If Not rsCountries.eof then
%>
<select class=content name="COUNTRY" tabindex="14" size="1">
<%
flag = 0
Do While not rsCountries.Eof
<%
If ucase(Request.form("countr y")) = ucase(rsCountries("COUNTRY _NAME")) Then
flag = 1
%>
<option value="<%=rsCountries("COU NTRY_NAME" )%>" selected><%=rsCountries("C OUNTRY_NAM E")%></opt ion>
<%
end if
%>
<option value="<%=rsCountries("COU NTRY_NAME" )%>"><%=rs Countries( "COUNTRY_N AME")%></o ption>
<%
if flag=0 then
%>
<option value="USA" selected>USA</option>
<%
end if
rsCountries.MoveNext
Loop
%>
</select>
<%
If Not rsCountries.eof then
%>
<select class=content name="COUNTRY" tabindex="14" size="1">
<%
flag = 0
Do While not rsCountries.Eof
<%
If ucase(Request.form("countr
flag = 1
%>
<option value="<%=rsCountries("COU
<%
end if
%>
<option value="<%=rsCountries("COU
<%
if flag=0 then
%>
<option value="USA" selected>USA</option>
<%
end if
rsCountries.MoveNext
Loop
%>
</select>
ASKER
hey casstd and alorentz,
I tried both the codes from above and page refuses to load. Isnt there a simple way of making the form start at UNITED STATES so that it has countries above it and below it ? i've seen it on many websites.
I tried both the codes from above and page refuses to load. Isnt there a simple way of making the form start at UNITED STATES so that it has countries above it and below it ? i've seen it on many websites.
My code works, but only puts USA at the top of the dropdown by default. No reason why that should be good enough....
ASKER
hey alorentz, do i just stick in the code ??? or do i have to change anything anywhere else ? the code looks like this right now !!! at the top of the page we have also included another accregiesterstart.asp file !! i have also posted the errors below the code !!
<tr>
<td align="left" width="148"><font class = contbold>Country:</font></ td>
<td width="299"><font class = content>
<%
If Not rsCountries.eof then
%>
<select class=content name="COUNTRY" tabindex="14" size="1">
<%
flag = 0
Do While not rsCountries.Eof
<%
If ucase(Request.form("countr y")) = ucase(rsCountries("COUNTRY _NAME")) Then
flag = 1
%>
<option value="<%=rsCountries("COU NTRY_NAME" )%>" selected><%=rsCountries("C OUNTRY_NAM E")%></opt ion>
<%
end if
%>
<option value="<%=rsCountries("COU NTRY_NAME" )%>"><%=rs Countries( "COUNTRY_N AME")%></o ption>
<%
if flag=0 then
%>
<option value="UNITED STATES" selected>UNITED STATES</option>
<%
end if
rsCountries.MoveNext
Loop
%>
</select>
<%End If%>
</td>
</tr>
<tr>
<td align="left" width="148"><font class = content>Zip/Postal Code:</font></td>
<td width="299">
<input type="text" name="ZIPCODE" size="8" value="<%=Request.Form("ZI PCODE")%>" tabindex="15" Class="Content">
<font class="contsmall"> (M andatory for USA)</font>
</td>
</tr>
<%If bSecretQues Then%>
<tr>
<td><font class=contbold>Secret Question:</font></td>
<td>
<font class=content><i>(Asked if you forget your password.For example:"What is your favourite color?")</i></font><br>
<font class=content>
<select name="SECRET_QUESTION" tabindex="18" Class="Content">
<%=sSecretQuestionHTML%>
</select>
</font>
</td>
</tr>
These are the errors i get !!!
Error Type:
Microsoft VBScript compilation (0x800A0400)
Expected statement
/accregister.asp, line 230
Browser Type:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Page:
GET /accregister.asp
Time:
Wednesday, September 08, 2004, 12:30:55 PM
Thanks !!
<tr>
<td align="left" width="148"><font class = contbold>Country:</font></
<td width="299"><font class = content>
<%
If Not rsCountries.eof then
%>
<select class=content name="COUNTRY" tabindex="14" size="1">
<%
flag = 0
Do While not rsCountries.Eof
<%
If ucase(Request.form("countr
flag = 1
%>
<option value="<%=rsCountries("COU
<%
end if
%>
<option value="<%=rsCountries("COU
<%
if flag=0 then
%>
<option value="UNITED STATES" selected>UNITED STATES</option>
<%
end if
rsCountries.MoveNext
Loop
%>
</select>
<%End If%>
</td>
</tr>
<tr>
<td align="left" width="148"><font class = content>Zip/Postal Code:</font></td>
<td width="299">
<input type="text" name="ZIPCODE" size="8" value="<%=Request.Form("ZI
<font class="contsmall"> (M
</td>
</tr>
<%If bSecretQues Then%>
<tr>
<td><font class=contbold>Secret Question:</font></td>
<td>
<font class=content><i>(Asked if you forget your password.For example:"What is your favourite color?")</i></font><br>
<font class=content>
<select name="SECRET_QUESTION" tabindex="18" Class="Content">
<%=sSecretQuestionHTML%>
</select>
</font>
</td>
</tr>
These are the errors i get !!!
Error Type:
Microsoft VBScript compilation (0x800A0400)
Expected statement
/accregister.asp, line 230
Browser Type:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Page:
GET /accregister.asp
Time:
Wednesday, September 08, 2004, 12:30:55 PM
Thanks !!
The code I gave should work just as is....but I don't know what line is 230? When posting error messages, makes ure you post the line that is specified in code.
ASKER
alorentz the line thats causing the error is
<% If ucase(Request.form("countr y")) = ucase(rsCountries("COUNTRY _NAME"))Th en flag = 1
thanks in advance
<% If ucase(Request.form("countr
thanks in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Any luck!
Heh, heh....ee_ai_construct, are you following me now? ;)
<option
value="<%=rsCountries("COU
<%If strComp(rsCountries("COUNT
Response.Write " SELECTED"
End If%>><%=rsCountries("COUNT
Or change "USA" to whatever it is in the db....,ie Americca, United States, etc...
-Matt