PDA

View Full Version : Session Variables and Drop Lists


rusa21
05-16-2010, 11:05 AM
I wrote a .asp form with 3 text boxes on it, intending for users to enter the appropriate data, and have the form post to another .asp page that creates session variables out of the input from the 3 textboxes. This works flawlessly. I then decided to change one of the text boxes to a dynamic drop list, so that the users couldn’t accidentally enter an invalid account number in the first box. Now they have to select one of the existing account numbers from the list, and type a name and phone number in the other two boxes. The problem is, now that the first text box is a drop list, the session variable creation is not happening when you submit the form. Below is an example of the code I used:

From the first page that used to have 3 textboxes, and now has 1 drop list and 2 text boxes: (this code queries the account number list from the DB file and populates the drop list from it, then writes the form to the .asp page and waits for the user to submit it.)

Set objRs = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT distinct(newSSID) FROM tblCallCenter "
objRS.Open strSQL, objconn
Response.Write "<form action=" & "'support2a.asp'" & "method=" & "'post'" & ">"
Response.Write "Network Name: "
Response.Write "<select name=" & "'list01'" & "id=" & "'list01'" & ">" & "<option value='''' selected>Pick One</option>"
Do While Not objRS.EOF
Response.Write "<option value=''" & objrs("newSSID") &"''>"& objRs("newSSID") &"</option>"
objRS.MoveNext
Loop
Response.Write "</select>" & "<a href=" & "'#network'" & ">" & " Click here if they don't know it" & "</a>" & "<br>"
Response.Write "Customer Name: "
Response.Write "<input name=" & "'txtName'" & "type=" & "'text'" & "id=" & "'txtName'" & ">" & "<br>"
Response.Write "Customer Phone: "
Response.Write "<input name=" & "'txtPhone'" & "type=" & "'text'" & "id=" & "'txtPhone'" & ">"
Response.Write "(we prefer a cell phone if possible)" & "<br>"
Response.Write "<input type=" & "'submit'" & "name=" & "'Submit'" & "value=" & "'Submit'" & ">"
Response.Write "</form>"
objRs.Close
objconn.Close

From the receiving page, the code that sets up the session variables:

<%session("name")=Request.Form("txtName")%>
<%session("phone")=Request.Form("txtPhone")%>
<%session("network")=Request.Form("list01")%>

I don’t care how many times I try to research this online, nobody has the solution in any online posting.

To test the creation of these variables, I appended the following code to the end of the second page:

Response.Write "Session Variable name is set to:"
Response.Write session("name")
Response.Write "<br>"
Response.Write "Session Variable phone is set to:"
Response.Write session("phone")
Response.Write "<br>"
Response.Write "Session Variable network is set to:"
Response.Write session("network")

The third session variable (“network”) is null. If I change the field on the first page back to a text box and type the account number in manually, all works perfectly. If I try to read the value of the drop list in using the <%session("network")=Request.Form("list01")%> code, it completely ignores the value of the drop list, the session variable is null, and my application fails.

9 hours researching on this so far and no luck...

Old Pedant
05-17-2010, 12:44 AM
Why why why do you create all the HTML code using Response.Write???

It's harder to write, harder to read, certainly way harder for somebody to maintain in the future. AND it's measurably slower.

SO the first thing I'd do is rewrite it to be READABLE.

<form action="support2a.asp" method="post">
Network Name:
<select name="list01">
<option value="" selected>Pick One</option>
<%
strSQL = "SELECT distinct newSSID FROM tblCallCenter "
Set objRS = objconn.Execute(strSQL) ' a bit faster and of course simpler
Do While Not objRS.EOF
%>
<option><%=objRs("newSSID")%></option>
<%
objRS.MoveNext
Loop
objRS.Close
%>
</select>
<a href="#network">Click here if they don't know it</a>
Customer Name: <input name="txtName" type="text"><br>
Customer Phone: <input name="txtPhone" type="text"> (we prefer a cell phone if possible)<br>
<input type="submit" name="Submit" value="Submit">
</form>


Other things to note:
-- There is almost never a reason to give an ID to form fields. You must give a form field a name, else it won't be sent to the next page (HTML rule, not ASP). But the only use for an ID would be if you needed it for a <label> tag.
-- If the value and text of an <option> are the same, you don't need to give the value. HTML sends the text when the value is omitted. (Again, HTML rule, not ASP).
-- SELECT DISTINCT applies to *ALL* fields of the SELECT. You can't just apply it to one field by putting parentheses around that field name. So it's a bad idea to get in the habit of using parens after DISTINCT. (Doesn't hurt in this case because there is only one field in the SELECT list...but try to avoid bad habits.)

Having said ALL the above...

Nope, I don't know why you don't get the <select>'s value on the next page.

But I know how to debug.

For starters, try putting this at the top of your target ASP page:

<%
For Each fname In Request.Form
Response.Write fname & "::" & Request.Form(fname) & "<br>" & vbNewLine
Next
%>
<hr>

What does that show you? It will dump out all the fields sent by HTML to the ASP page.

Note that you can also do
<%
For Each fname In Session.Contents
Response.Write fname & "::" & Session(fname) & "<br>" & vbNewLine
Next
%>
<hr>

to find all session variables.

Old Pedant
05-17-2010, 12:59 AM
One thing that strike me is that because of the ugly Response.Write coding you are creating illegal HTML.

For example, your line that does
Response.Write "<select name=" & "'list01'" & "id=" & "'list01'" & ">" & "<option value='''' selected>Pick One</option>"

will produce this HTML:

<select name='list01'id='list01'><option value='''' selected>Pick One</option>

(1) You are using '...' around the name and id identifiers. If you are using old-style HTML, that's legal (though frowned upon). If your page is XHTML, that's not even legal (you must use "..." to be compliant).
(2) You have NO SPACE between 'list01' and id. Again, HTML might allow that (though frowned upon).
(3) Your value for that supposedly blank option is *NOT* legal in any form. It consists of four apostrophes in a row. In old style HTML, the first two would be okay, but then the second two would be meaningless. Not sure what you were trying to do, there.

All of this just points out some of the dangers of using Response.Write to create HTML. It's so very error prone that possibly there are other mistakes that I missed.

rusa21
05-17-2010, 04:10 PM
Please forgive my awful code...I did piece it together from various articles I found on here and a few other sites. I made the changes, and now the code works correctly. I assume that using the Response.Write commands to make the form on the fly somehow caused the Select box to not be defined at the time the variable was passed...or something along those lines. Now that the select is generated from a proper form code, it works beautifully. I GREATLY appreciate your help!

Old Pedant
05-17-2010, 06:59 PM
Interesting. I'm still curious why it didn't work (assuming the page is HTML and not XHTML), but not enough so to go try to reproduce it.

The code was mildly ugly (I've seen a lot worse!), but I didn't see any glaring errors. (Again, assuming the page was HMTL.)