Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Persisting <option value="">

Status
Not open for further replies.

iRead

Programmer
Mar 12, 2004
25
US
I have an application with a portion that essentially acts as a contact manager. After a new contact is added the data is save in the database and the page reloads to clear the values in order to except new values for the next contact to add. This method has worked fine. Now I want to persist some values from the last contact to the new entry form. One of the fields is a dropdown list populated from a database table.

This is a portion of the code:

<%
strSQL1 = "SELECT areaID, areaName FROM Areas ORDER BY areaName"
Set getRSRecords = nothing
Set getRSRecords = conn.Execute(strSQL1)
getRecords = getRSRecords.getRows
numRows = ubound(getRecords, 2)
%>
<select name="areaID">
<option value="">( Select an Area )</option>
<% for counter = 0 to numRows %>
<option value="<% =getRecords(0, counter) %>"><% =getRecords(1, counter) %></option>
<% next %>
</select></font><br>

I’ll admit I’ve tried a few things with out any successes and any input will be greatly appreciated.

Thank you!
 
we need the word "selected" in the last selected option:

Code:
<select name="areaID">
<option value="">( Select an Area )</option>
<% for counter = 0 to numRows %>
<option value="<% =getRecords(0, counter) %>"

if rtrim(request.form("areaID")) = rtrim(getRecords(0, counter)) then response.write " selected "

><% =getRecords(1, counter) %></option>
<% next %>
</select>

ttmug.gif
 
Thank you for your input foxbox!

I must still be missing something. When I implemented the change you’d suggested the default selection when the page loads is the last record from the database table. I was happy to see something had changed in the behavior of the page but this is still not what I was trying to achieve. Do you have any additional wise guidance for me.

Thankfully iRead
 
Let's try this:

Code:
<select name="areaID">
<option value="">( Select an Area )</option>

<%
dim cAreaID
cAreaID = rtrim(request.form("areaID"))

cSQL = "SELECT areaID, areaName FROM Areas " &_
       "ORDER BY areaName"
Set oRS = Server.CreateObject("ADODB.Recordset")
oRS.Open cSql, Conn

do while not oRS.eof
 response.write "<option value='" & oRS(0) & "'"
 if cAreaId = rtrim(oRS(0)) then response.write " selected "
 response.write ">" & oRS(1) & "</option>"
 oRS.movenext
loop
oRS.close
set oRS = nothing
%>
</select>

ttmug.gif
 
Ok foxbox, I think we’re getting close. I don’t think I made clear that the page containing this code post the entered data then redirects to another page. From that page we have the option to add another new contact. If the user elects to add another new contact the page containing this code reloads. I think the solution may be to make the variable we declare a session variable. What do you think?

Enthusiastically Thank you,
iRead
 
Yep. that's a way to do that.
<select name="areaID">
<option value="">( Select an Area )</option>

<%
cSQL = "SELECT areaID, areaName FROM Areas " &_
"ORDER BY areaName"
Set oRS = Server.CreateObject("ADODB.Recordset")
oRS.Open cSql, Conn

do while not oRS.eof
response.write "<option value='" & oRS(0) & "'"
if rtrim(session("sAreaID")) = rtrim(oRS(0)) then
response.write " selected "
end if
response.write ">" & oRS(1) & "</option>"
oRS.movenext
loop
oRS.close
set oRS = nothing
%>
</select>
[/code]


otherpage.asp:
Code:
session("sAreaID") = request.form("areaID")
if session("sAreaID") = "" then
 ' error: no area selected!
 ' response.redirect original_page.asp
 response.end
end if

ttmug.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top