Link to home
Start Free TrialLog in
Avatar of agodil
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("country")) = ucase(rsCountries("COUNTRY_NAME")) Then%>
<option value="<%=rsCountries("COUNTRY_NAME")%>" selected><%=rsCountries("COUNTRY_NAME")%></option>
<%Else%>
<option value="<%=rsCountries("COUNTRY_NAME")%>"><%=rsCountries("COUNTRY_NAME")%></option>
<%End If
rsCountries.MoveNext
Loop
Else
Do While not rsCountries.Eof%>
<option value="<%=rsCountries("COUNTRY_NAME")%>"><%=rsCountries("COUNTRY_NAME")%></option>
<%rsCountries.MoveNextLoop
End If%>
</select>
<%End If%>
</td>
</tr>
<tr>

Please help !
                       
Avatar of mattfairw
mattfairw

change the option loop to:

<option
  value="<%=rsCountries("COUNTRY_NAME")%>"
  <%If strComp(rsCountries("COUNTRY_NAME"),"USA",1) = 0 Then
    Response.Write " SELECTED"
  End If%>><%=rsCountries("COUNTRY_NAME")%></option>

Or change "USA" to whatever it is in the db....,ie Americca, United States, etc...

-Matt
Avatar of agodil

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("COUNTRY_NAME")%>"><%=rsCountries("COUNTRY_NAME")%></option>
<%rsCountries.MoveNextLoop
End If%>

so i changed it to
Do While not rsCountries.Eof%>
<option
  value="<%=rsCountries("COUNTRY_NAME")%>"
  <%If strComp(rsCountries("COUNTRY_NAME"),"USA",1) = 0 Then
    Response.Write " SELECTED"
  End If%>><%=rsCountries("COUNTRY_NAME")%></option>
</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("COUNTRY_NAME")%>"
  <%If rsCountries("COUNTRY_NAME") == "USA" Then
    Response.Write " SELECTED"
  End If%>><%=rsCountries("COUNTRY_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,
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("COUNTRY_NAME")%>"
<%
        If strComp(rsCountries("COUNTRY_NAME"),"USA",1) = 0 Then
            Response.Write " SELECTED"
        End If
%>><%=rsCountries("COUNTRY_NAME")%></option>
<%
    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("COUNTRY_NAME").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.  :-)
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("country")) = ucase(rsCountries("COUNTRY_NAME")) Then%>
                         selected = " selected"
      else
                          selected = ""
      end if
          <option value="<%=rsCountries("COUNTRY_NAME")%>" selected><%=rsCountries("COUNTRY_NAME")%></option>
            
           <%  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("COUNTRY_NAME")%>" selected><%=rsCountries("COUNTRY_NAME")%></option>
      <%   rsCountries.MoveNextLoop
           End If%>
      </select>
<%End If%>
</td>
</tr>
<tr>

Hope this would be helpful to you.
Avatar of agodil

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
Avatar of agodil

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 !!
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("country")) = ucase(rsCountries("COUNTRY_NAME")) Then%>
                         selected = " selected"
     else
                          selected = ""
     end if
          <option value="<%=rsCountries("COUNTRY_NAME")%>" selected><%=rsCountries("COUNTRY_NAME")%></option>
         
           <%  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("COUNTRY_NAME")%>" selected><%=rsCountries("COUNTRY_NAME")%></option>
    <%  
            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("country")) = ucase(rsCountries("COUNTRY_NAME")) Then
            flag = 1
           %>
           <option value="<%=rsCountries("COUNTRY_NAME")%>" selected><%=rsCountries("COUNTRY_NAME")%></option>
           <%
        end if
%>
<option value="<%=rsCountries("COUNTRY_NAME")%>"><%=rsCountries("COUNTRY_NAME")%></option>
<%
if flag=0 then
%>
<option value="USA" selected>USA</option>
<%
end if
rsCountries.MoveNext
Loop
%>
</select>
Avatar of agodil

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.
My code works, but only puts USA at the top of the dropdown by default.  No reason why that should be good enough....
Avatar of agodil

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("country")) = ucase(rsCountries("COUNTRY_NAME")) Then
            flag = 1
           %>
           <option value="<%=rsCountries("COUNTRY_NAME")%>" selected><%=rsCountries("COUNTRY_NAME")%></option>
           <%
        end if
%>
<option value="<%=rsCountries("COUNTRY_NAME")%>"><%=rsCountries("COUNTRY_NAME")%></option>
<%
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("ZIPCODE")%>" tabindex="15" Class="Content">
                                                                        <font class="contsmall">&nbsp;(Mandatory 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 !!
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.
Avatar of agodil

ASKER

alorentz the line thats causing the error is
  <% If ucase(Request.form("country")) = ucase(rsCountries("COUNTRY_NAME"))Then flag = 1
thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of alorentz
alorentz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Any luck!
Heh, heh....ee_ai_construct, are you following me now? ;)