We help IT Professionals succeed at work.

Classic ASP VBScript:  How to get a plain text field to retain data after page reload.

lshane
lshane asked
on
1,320 Views
Last Modified: 2008-02-01
DWMX
MS Access
Classic ASP VBSCript
WIN XP PRO

Hello.  Would someone, please, assist me with getting a text field to retain its value on a page refresh?
I'm not ready to try an AJAX solution just yet.

This is a form that involves cities, states, and counties.  I have the "state" and "city" dropdowns with a Jumpmenu to the same page... causing it to refresh.  When the "state" is selected, it reloads the page and  populates that state's cities in the "city" dropdown".  When the "city" is selected, it reloads the page again and populates the "counties".  I've been able to get the dropdowns to retain their value by passing variables (i.e. "?abb=whatever") and then with an <%IF... THEN%> statement for the SELECTION.

However, I am not able to get a plain text field to retain its value.

There will be more plain text fields on the real form, but here is a test page to view:  <http://www.thebargainwatcher.com/dropdown_test2.asp>

Please help.

Thanks so much,
Shane
Comment
Watch Question

b0lsc0ttIT Manager
CERTIFIED EXPERT

Commented:
What do you mean "plain text field"?  Do you mean a text area (i.e. <textarea>)?

If so use ...

<textarea name="area1"><%= strArea %></textarea>

The value needs to go between the textarea opening and closing tags.

Let me know if you have a question.

bol

Author

Commented:
Hi, b0lsc0tt.  No... just an <input type="text"...> field.

Were you able to look at the link:  <http://www.thebargainwatcher.com/dropdown_test2.asp>

The "Test1" field at the top.


Thanks,
Shane
b0lsc0ttIT Manager
CERTIFIED EXPERT

Commented:
Hey. :)

I had not looked at it but I have now.  What was the problem with something like below?

<input name="test1" type="text" id="test1" value="<%= Request.Form("test1") %>">

That is the way you would "fill" the value in the form with ASP script.  If that doesn't work then look at your html source to see if you something there.  Do you get an error?  I haven't tried submitting the form and testing that myself but let me know if I can do that.

bol
b0lsc0ttIT Manager
CERTIFIED EXPERT

Commented:
I just looked at the form again and I can't test that page since it has no submit button.  

If you still have a problem, where does the value come from?  Is it also a form?  Does it also use post as the method?  What is the field's name?  You might be having the form post to itself and, if so, my code above will work for it.  Let me know though.

bol

Author

Commented:
Hi, b0lsc0tt.  No worries.  Sorry - the dropdowns are actually Jumpmenu's, and I have them redirecting back to the same page.  You have to select a state and it will direct back to itself - then you can see the value disappear from the field.  There are 3 recordsets on the page (1 for "states" and 1 for "cities" and 1 for "counties").  I tried your suggestion last night, and again when you suggested it a minute ago, and it still does not retain whatever I type in after I make a selection from the "state" menu.

Would you mind if I pasted the code?

Shane
b0lsc0ttIT Manager
CERTIFIED EXPERT

Commented:
That would be fine and helpful.  If there is a lot then you can zip the file and upload it to www.ee-stuff.com.  That site is just for EE members but let me know if you have a question or concern about it.

bol
b0lsc0ttIT Manager
CERTIFIED EXPERT

Commented:
I think the problem is not using the form to submit when you go to the next page.  You are just using Javascript, the URL and changing the page.  However the URL doesn't have the value from text1 and the form isn't sent.  Also the ASP script looks for a post form and not at the URL.  Do you want to submit the form?  If so change the script that "changes" the page to something like ...

document.forms[0].submit();

That line will submit the form and all the fields will be sent.  I have to take off for a bit but I can look at your html closer and try to be more specific about the change.

bol

Author

Commented:
Well, I am wanting to submit... eventually, at the end of the form, but I wanted the address info at the top, after the fields that will the the "Title" and "Type".

I tried to combine a "Request" variable for "Test1" with the State's "onchange" so it would be passed in the URL, but it still did not seem to work.  I'll post the code, and maybe you can see another angle (Hopefully it's not too much):

=================================================================================
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="Connections/conn_bargaindata.asp" -->
<%
Dim rsCities__MMColParam
rsCities__MMColParam = "1"
If (Request("abb") <> "") Then
  rsCities__MMColParam = Request("abb")
End If
%>
<%
Dim rsCities
Dim rsCities_numRows

Set rsCities = Server.CreateObject("ADODB.Recordset")
rsCities.ActiveConnection = MM_conn_bargaindata_STRING
rsCities.Source = "SELECT *  FROM cities  WHERE stateabbreviation = '" + Replace(rsCities__MMColParam, "'", "''") + "'"
rsCities.CursorType = 0
rsCities.CursorLocation = 2
rsCities.LockType = 1
rsCities.Open()

rsCities_numRows = 0
%>
<%
Dim rsCounties__MMColParam
rsCounties__MMColParam = "1"
If (Request("city") <> "") Then
  rsCounties__MMColParam = Request("city")
End If
%>
<%
Dim rsCounties__MMColParam2
rsCounties__MMColParam2 = "1"
If (Request("abb") <> "") Then
  rsCounties__MMColParam2 = Request("abb")
End If
%>
<%
Dim rsCounties
Dim rsCounties_numRows

Set rsCounties = Server.CreateObject("ADODB.Recordset")
rsCounties.ActiveConnection = MM_conn_bargaindata_STRING
rsCounties.Source = "SELECT *  FROM cities_counties  WHERE city = '" + Replace(rsCounties__MMColParam, "'", "''") + "'  AND stateabbreviation = '" + Replace(rsCounties__MMColParam2, "'", "''") + "'"
rsCounties.CursorType = 0
rsCounties.CursorLocation = 2
rsCounties.LockType = 1
rsCounties.Open()

rsCounties_numRows = 0
%>
<!--#include file="dwzFormControls/FormControl.asp" -->
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
//-->
</script>

</head>

<body>
<table width="400" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>



<%
if request.querystring("city") <> "" then
             storeCity = request("city")
End If
%>
<%
if request.querystring("abb") <> "" then
             storeAbbState = trim(UCASE(request.querystring("abb")))
End If
%>


<form name="form1" method="post" action="">
    <table width="400" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td>Test 1:</td>
        <td>

<input name="test1" type="text" id="test1"></td>
      </tr>
      <tr>
        <td width="100">State:</td>
        <td width="300">

<select name="state" id="state" onChange="MM_jumpMenu('parent',this,0)">
        <option selected>-:State:-</option>
        <option value="dropdown_test2.asp?abb=AK" <%If storeAbbState = "AK" then response.write "SELECTED" End If%>>AK</option>
        <option value="dropdown_test2.asp?abb=AL" <%If storeAbbState = "AL" then response.write "SELECTED" End If%>>AL</option>
        <option value="dropdown_test2.asp?abb=AR" <%If storeAbbState = "AK" then response.write "SELECTED" End If%>>AR</option>
        <option value="dropdown_test2.asp?abb=AZ" <%If storeAbbState = "AR" then response.write "SELECTED" End If%>>AZ</option>
        <option value="dropdown_test2.asp?abb=CA" <%If storeAbbState = "CA" then response.write "SELECTED" End If%>>CA</option>
        <option value="dropdown_test2.asp?abb=CO" <%If storeAbbState = "CO" then response.write "SELECTED" End If%>>CO</option>
        <option value="dropdown_test2.asp?abb=CT" <%If storeAbbState = "CT" then response.write "SELECTED" End If%>>CT</option>
        <option value="dropdown_test2.asp?abb=DE" <%If storeAbbState = "DE" then response.write "SELECTED" End If%>>DE</option>
        <option value="dropdown_test2.asp?abb=FL" <%If storeAbbState = "FL" then response.write "SELECTED" End If%>>FL</option>
        <option value="dropdown_test2.asp?abb=GA" <%If storeAbbState = "GA" then response.write "SELECTED" End If%>>GA</option>
        <option value="dropdown_test2.asp?abb=HI" <%If storeAbbState = "HI" then response.write "SELECTED" End If%>>HI</option>
        <option value="dropdown_test2.asp?abb=IA" <%If storeAbbState = "IA" then response.write "SELECTED" End If%>>IA</option>
        <option value="dropdown_test2.asp?abb=ID" <%If storeAbbState = "ID" then response.write "SELECTED" End If%>>ID</option>
        <option value="dropdown_test2.asp?abb=IL" <%If storeAbbState = "IL" then response.write "SELECTED" End If%>>IL</option>
        <option value="dropdown_test2.asp?abb=IN" <%If storeAbbState = "IN" then response.write "SELECTED" End If%>>IN</option>
        <option value="dropdown_test2.asp?abb=KS" <%If storeAbbState = "KS" then response.write "SELECTED" End If%>>KS</option>
        <option value="dropdown_test2.asp?abb=KY" <%If storeAbbState = "KY" then response.write "SELECTED" End If%>>KY</option>
        <option value="dropdown_test2.asp?abb=LA" <%If storeAbbState = "LA" then response.write "SELECTED" End If%>>LA</option>
        <option value="dropdown_test2.asp?abb=MA" <%If storeAbbState = "MA" then response.write "SELECTED" End If%>>MA</option>
        <option value="dropdown_test2.asp?abb=MD" <%If storeAbbState = "MD" then response.write "SELECTED" End If%>>MD</option>
        <option value="dropdown_test2.asp?abb=ME" <%If storeAbbState = "ME" then response.write "SELECTED" End If%>>ME</option>
        <option value="dropdown_test2.asp?abb=MI" <%If storeAbbState = "MI" then response.write "SELECTED" End If%>>MI</option>
        <option value="dropdown_test2.asp?abb=MN" <%If storeAbbState = "MN" then response.write "SELECTED" End If%>>MN</option>
        <option value="dropdown_test2.asp?abb=MO" <%If storeAbbState = "MO" then response.write "SELECTED" End If%>>MO</option>
        <option value="dropdown_test2.asp?abb=MS" <%If storeAbbState = "MS" then response.write "SELECTED" End If%>>MS</option>
        <option value="dropdown_test2.asp?abb=MT" <%If storeAbbState = "MT" then response.write "SELECTED" End If%>>MT</option>
        <option value="dropdown_test2.asp?abb=NC" <%If storeAbbState = "NC" then response.write "SELECTED" End If%>>NC</option>
        <option value="dropdown_test2.asp?abb=ND" <%If storeAbbState = "ND" then response.write "SELECTED" End If%>>ND</option>
        <option value="dropdown_test2.asp?abb=NE" <%If storeAbbState = "NE" then response.write "SELECTED" End If%>>NE</option>
        <option value="dropdown_test2.asp?abb=NH" <%If storeAbbState = "NH" then response.write "SELECTED" End If%>>NH</option>
        <option value="dropdown_test2.asp?abb=NJ" <%If storeAbbState = "NJ" then response.write "SELECTED" End If%>>NJ</option>
        <option value="dropdown_test2.asp?abb=NM" <%If storeAbbState = "NM" then response.write "SELECTED" End If%>>NM</option>
        <option value="dropdown_test2.asp?abb=NV" <%If storeAbbState = "NV" then response.write "SELECTED" End If%>>NV</option>
        <option value="dropdown_test2.asp?abb=NY" <%If storeAbbState = "NY" then response.write "SELECTED" End If%>>NY</option>
        <option value="dropdown_test2.asp?abb=OH" <%If storeAbbState = "OH" then response.write "SELECTED" End If%>>OH</option>
        <option value="dropdown_test2.asp?abb=OK" <%If storeAbbState = "OK" then response.write "SELECTED" End If%>>OK</option>
        <option value="dropdown_test2.asp?abb=OR" <%If storeAbbState = "OR" then response.write "SELECTED" End If%>>OR</option>
        <option value="dropdown_test2.asp?abb=PA" <%If storeAbbState = "PA" then response.write "SELECTED" End If%>>PA</option>
        <option value="dropdown_test2.asp?abb=RI" <%If storeAbbState = "RI" then response.write "SELECTED" End If%>>RI</option>
        <option value="dropdown_test2.asp?abb=SC" <%If storeAbbState = "SC" then response.write "SELECTED" End If%>>SC</option>
        <option value="dropdown_test2.asp?abb=SD" <%If storeAbbState = "SD" then response.write "SELECTED" End If%>>SD</option>
        <option value="dropdown_test2.asp?abb=TN" <%If storeAbbState = "TN" then response.write "SELECTED" End If%>>TN</option>
        <option value="dropdown_test2.asp?abb=TX" <%If storeAbbState = "TX" then response.write "SELECTED" End If%>>TX</option>
        <option value="dropdown_test2.asp?abb=UT" <%If storeAbbState = "UT" then response.write "SELECTED" End If%>>UT</option>
        <option value="dropdown_test2.asp?abb=VA" <%If storeAbbState = "VA" then response.write "SELECTED" End If%>>VA</option>
        <option value="dropdown_test2.asp?abb=VT" <%If storeAbbState = "VT" then response.write "SELECTED" End If%>>VT</option>
        <option value="dropdown_test2.asp?abb=WA" <%If storeAbbState = "WA" then response.write "SELECTED" End If%>>WA</option>
        <option value="dropdown_test2.asp?abb=WI" <%If storeAbbState = "WI" then response.write "SELECTED" End If%>>WI</option>
        <option value="dropdown_test2.asp?abb=WV" <%If storeAbbState = "WV" then response.write "SELECTED" End If%>>WV</option>
        <option value="dropdown_test2.asp?abb=WY" <%If storeAbbState = "WY" then response.write "SELECTED" End If%>>WY</option>
        </select></td>
      </tr>
      <tr>
        <td>City:</td>
        <td>

<select name="form_city" id="form_city" onChange="MM_jumpMenu('parent',this,0)">
<option selected>--Select City--</option>
<%
While (NOT rsCities.EOF)
%>
<option value="dropdown_test2.asp?city=<%=(rsCities.Fields.Item("city").Value)%>&abb=<%=(rsCities.Fields.Item("stateabbreviation").Value)%>" <%If (Not isNull(Request("city"))) Then If (CStr(rsCities.Fields.Item("city").Value) = CStr(Request("city"))) Then Response.Write("SELECTED") : Response.Write("")%> ><%=(rsCities.Fields.Item("city").Value)%></option>
  <%
  rsCities.MoveNext()
Wend
If (rsCities.CursorType > 0) Then
  rsCities.MoveFirst
Else
  rsCities.Requery
End If
%>
        </select></td>
      </tr>
      <tr>
        <td>County:</td>
        <td>
<select name="county" id="county">
<option selected>--Select County--</option>
          <%
While (NOT rsCounties.EOF)
%>
          <option value="<%=(rsCounties.Fields.Item("county").Value)%>"><%=(rsCounties.Fields.Item("county").Value)%></option>
          <%
  rsCounties.MoveNext()
Wend
If (rsCounties.CursorType > 0) Then
  rsCounties.MoveFirst
Else
  rsCounties.Requery
End If
%>
        </select></td>
      </tr>
    </table>
    </form></td>
  </tr>
</table>
</body>
</html>
<%
rsCities.Close()
Set rsCities = Nothing
%>
<%
rsCounties.Close()
Set rsCounties = Nothing
%>

=================================================================================

Thanks b0lsc0tt,
Shane
IT Manager
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi, b0lsc0tt.  That worked great!  Thank you.  Now, however, I'm in another quandry.

The objective is just to have this form (which will be a "sale" form for consignment shoppers) completed by the user and submitted and inserted into the MS Access db.  The top of the form will have a Title for the sale, and then the user will enter the address.  Then other information below the address will be requested, as well.  Since the "State" and "City" dropdowns reload the page to populate the menus, it was clearing the text field, which would be for the Title (I changed our "Test1" to "Title").  Your solution worked great to retain that.

Now my other problem:  <http://www.thebargainwatcher.com/dropdown_test2.asp>

I added the SUBMIT button to submit to another page so I could see if the values were all transferring correctly... only the "Title" and "County" fields are transferring correctly.  The "State" and "City" are displaying the string I set for the "Request" variables (i.e. " dropdown_test2.asp?abb= ").

How can I get that to just show my selection from the menus?

Also, I am seeing where possibly AJAX may be a better solution.  I just don't know it very well and am not sure how to connect to the db to extract the data I need from the 3 tables (states, cities, and counties); but if you think that might be a better choice, then I'm open to that - just an option.

Otherwise, I just need to get these fields to transfer correctly.

Thanks so much,
Shane
b0lsc0ttIT Manager
CERTIFIED EXPERT

Commented:
I'm glad that solved this problem. :)

If you use a submit button and really submit the form then make sure the ASP on the page getting the form uses Request.Form() instead of just Request.  That should allow you to get all of the values.  This does assume you still have the method in the form tag as Post.  If that part still doesn't work then let me know.  It might be best to open a new question for it since it seems to be a different issue.  Of course you can post the URL to that new question here if you would like to get my attention on it but I try to keep an eye on all the questions in this topic (and a few others ;)).

AJAX is nice but I am not sure I understand why you need it here.  Are you trying to fill the state, cities, and/or counties lists dynamically without refreshing the page (i.e. choose a state and the cities select list is made based on the state chosen)?  If so that is a common use of AJAX and would be nice for the form.  I have a fair amount of experience with it and have answered many questions on it here.  Let me know if you want more details and open a question on it.   A search of this site using my member name and the term AJAX should provide lots of results too. :)

Let me know if you have any other questions on this.  I hope the issue on the new script is as simple as what I described above.

bol

Author

Commented:
Thanks, b0lsc0tt.  I will post another question and link it here.

I appreciate it!!!

Shane
b0lsc0ttIT Manager
CERTIFIED EXPERT

Commented:
Your welcome!  I'm glad I could help.  I'll take a look at the other question, when you post the link, and post if I can help.

Thanks for the fun question, the grade and the points.  See you around. :D

bol
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.