Link to home
Start Free TrialLog in
Avatar of flfmmqp
flfmmqp

asked on

Pass value through URL

I have a form where I am passing on an id in the URL to be used in the page to sort a select box (selDate) with.  When the users changes selDate to another value the form repost.  What I need to do is store the value either in a box or post it back in the url.  I don't want to lose it because I still need to query with it.  

<form action="DMA_Chart_DataTable2.asp?id=" & Server.URLEncode(txtGEOID) & "" method="post" name="frmDisplay" onSubmit="return submitForm()">

I have tried a varite of different ways to do this but I must be missing something.  Please help.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
 
<SCRIPT LANGUAGE=javascript>
<!--
 
function submitStores(){
	var objForm = document.forms[0];
	
	objForm.submit();
}
//-->
</SCRIPT>
 
<%
 
Dim strselDate
strselDate = Request.Form("selDate")
dim strGEOID, varGEOID
strGEOID = Request.QueryString("id")
 
//txtGEOID = strGEOID
 
%>
 
<% 
sub updateStores()       
 dim varGEOID
         sqltext = "SELECT CHART_A.STORES, CHART_A.DATE2 FROM CHART_A GROUP BY CHART_A.STORES, CHART_A.DATE2, CHART_A.GEOID HAVING (((CHART_A.DATE2)='" & strselDate & "') AND ((CHART_A.GEOID)='517')) ORDER BY CHART_A.DATE2;"
     If strselDate <> "" Then
            Response.Write("Date Chosen: " & strselDate & vbCrLf)
     
        dim strDate, strDate2
 
        Dim oRSbn
	    'Create the recordset object
	    Set oRSbn = Server.CreateObject("ADODB.Recordset")
        oRSbn.CursorType= 3 
        oRSbn.LockType = 1 
        oRSbn.CursorLocation = 3
	    'Open the recordset getting a list of all Divisions
        //sqltext = "SELECT CHART_A.STORES, CHART_A.DATE2 FROM CHART_A GROUP BY CHART_A.STORES, CHART_A.DATE2, CHART_A.GEOID HAVING (((CHART_A.DATE2)='01/2004') AND ((CHART_A.GEOID)='" & GEOID & "')) ORDER BY CHART_A.DATE2;"
        sqltext = "SELECT CHART_A.STORES, CHART_A.DATE2 FROM CHART_A GROUP BY CHART_A.STORES, CHART_A.DATE2, CHART_A.GEOID HAVING (((CHART_A.DATE2)='" & strselDate & "') AND ((CHART_A.GEOID)='517')) ORDER BY CHART_A.DATE2;"
        oRSbn.open sqltext, "DSN=CompEvents"
         'go to first record.
        oRSbn.MoveFirst
 
        do while not oRSbn.eof
            response.Write(oRSbn("STORES"))
           oRSbn.movenext
        loop
        oRSbn.close
        set oRSbn = nothing
    Else
        Response.Write("Select a date to see this info.")
    End If
end sub
%> 
<%
sub updateGEOID()
    response.Write(Request.QueryString("id"))
end sub
 %>
 
</head>
<body>
<form action="DMA_Chart_DataTable2.asp?id=" & Server.URLEncode(txtGEOID) & "" method="post" name="frmDisplay" onSubmit="return submitForm()">
  <input type="hidden" name="id" value="<% call updateGEOID %>">
<br />
 
    <TD style="width: 150px; height: 73px;"><STRONG><EM>Choose Date:<br />
        <SELECT name=selDate size=1 ID="selDate" onChange = "submitStores()">
<%     
	'Create the recordset object
	Set oRSbn = Server.CreateObject("ADODB.Recordset")
	'Open the recordset getting a list of all Dates for this Market
    'sqltext = "SELECT CHART_A.[Date2] FROM CHART_A GROUP BY CHART_A.GEOID, CHART_A.[Date2] HAVING (((CHART_A.GEOID)='517')) ORDER BY CHART_A.[Date2];"
    sqltext = "SELECT CHART_A.Date2 FROM CHART_A GROUP BY CHART_A.Date2, CHART_A.GEOID HAVING (((CHART_A.GEOID)='517')) ORDER BY CHART_A.[Date2];"
 
    oRSbn.open sqltext, "DSN=CompEvents"
     'go to first record.%>
 <option value="0">Select A Date</option><%
   oRSbn.MoveFirst
        do while not oRSbn.eof
%>        
<option value="<%=oRSbn("Date2")%>"> <%=oRSbn("Date2")%></option>
<%
        oRSbn.movenext
        loop
    oRSbn.close
    set oRSbn = nothing    
 %>   
    
    </SELECT></EM></STRONG></TD>
 
 
&nbsp;&nbsp;
    <input id="txtGEOID" name="txtGEOID" type="text" value="<% call updateGEOID %>"/><br />
 
     <textarea name ="StoresList" style="width: 933px; height: 356px;" > <% call updateStores%>  </textarea>
    <br />
    C:\Inetpub\wwwroot\CHART\DMA_Chart_DataTable2.asp
    
    
</form>
 
</body>
</html>

Open in new window

Avatar of rfportilla
rfportilla
Flag of United States of America image

Use a session cookie.  

document.cookie =
  'ppkcookie1=testcookie; path=/'

I pulled this example from http://www.quirksmode.org/js/cookies.html

A session cookie will stay alive as long as the current browser session is open, but will not survive after you close the browser.  Basically, have the value check if the cookie value exists and set it appropriately.  When the user updates the date, update the cookie value.  The difference between a session cookie and a persistent cookie is the whether it has an expiration on it.  If you want the cookie to persist, set an expiration using the example from the website.
Avatar of flfmmqp
flfmmqp

ASKER

I'm not sure how that would work.  Where would I even put that?  Must be an easier way then this.
Avatar of flfmmqp

ASKER

You would think something simple like one of these would work:
<form action="DMA_Chart_DataTable2.asp?id=<%Request.QueryString("id") %>" method="post" name="frmDisplay" onSubmit="return submitForm()">

<form action="DMA_Chart_DataTable2.asp?id=<%Request.Form("txtGEOID")  %>" method="post" name="frmDisplay" onSubmit="return submitForm()">
why not create a hidden field for the id value? that way it will always be submitted with the form.
I am not sure how to incorporate this into your html, but here is an example.  Notice three important parts.  1. the script functions in the <head></head> section. 2. the init function that is called from the <body> tag. 3. the <input> tag has an id and an onchange event.  These are important.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head onload="if (readCookie('SQL-Date')) {this.value = readCookie('SQL-Date')}" >
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
 
<script language="javascript" >
 
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
 
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
 
function init () {
	var ID = document.getElementById("dateval")
	if (ID && readCookie('SQL-Date')) {ID.value = readCookie('SQL-Date')}
}
</script>
 
</head>
 
<body onload="init()">
 
<form>
  Sample Date Value:
  <input type="text" id="dateval" onchange="createCookie('SQL-Date',this.value,0); alert(readCookie('SQL-Date'))" />
</form>
</body>
</html>

Open in new window

Yeah, a hidden field might work and are easier to include.  But, cookies are longer lasting and require less maintenance.  If someone goes through more than a couple of screens, you have to make sure you pass that hidden value forward each time.  But now you have two solutions.

The modification to the action will not work like that.  you have to create a hidden field to carry it forward.  Something like <input type=hidden value="document.%formname%.%fieldname%.value">.  I don't like this solution because, like I said above, if you have too many pages to go through it creates a management nightmare to try to remember to pass that field forward.  If you get in the habit of using cookies, the information will be available on any page at any time.  

There is an ASP way of doing this if you are more comfortable with ASP, but it is still using cookies.  

Let me know if you need anything else.
persistence wasn't part of his requirements... don't over complicate things.
Huh?  Sorry, Alien109, but I didn't think I was overcomplicating things.  I thought I was being thorough.  I think a hidden field is a good suggestion, but I still like cookies better and I gave my reasons why.  Let the author choose.  

Even with a hidden field, some javascript would be needed to update the value and the input field that the user sees.  The only difference is that I am storing the value as a cookie instead of a field.  
ASKER CERTIFIED SOLUTION
Avatar of rfportilla
rfportilla
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
Hello, flfmmqp.  Any update on this?  Did this work for you?
Avatar of flfmmqp

ASKER

Thanks for the help.  That was very helpful