Link to home
Start Free TrialLog in
Avatar of magoo2
magoo2

asked on

Passing Data Between Pages

Hi all,

 I have a page which loads data from a database into a table. I would like to take the value of the cell clicked (date) and pass it to another page.

This is what i have so far on the code.

<%  Do Until rsCustomersList.EOF%>
 

<tr>
  <td bgcolor="f7efde" align=center>
    <%= rsCustomersList("FirstName")%>  
    <%= " " %>
    <%= rsCustomersList("LastName") %>
  </td>
  <td bgcolor="f7efde" align=center>
    <%= rsCustomersList("Username")%>  
  </td>

    <td bgcolor="f7efde" align=center>
    <% astring = "<A HREF=bydate.asp?action=SetDate=" & rsCustomersList("Date") & " </A>" %>
    <%= astring %>  
    <%= rsCustomersList("Date")%>  
    </td>

</tr>



<%
rsCustomersList.MoveNext
count = count + 1
Loop

%>

Thanks for any help

Magoo

Avatar of TTom
TTom

I think I see what you are trying to do.  I have no reason to believe it won't work, but you need to close your <a> tag and provide some text for the link.  Perhaps something like:

<% astring = "<A HREF=bydate.asp?action=SetDate=" & rsCustomersList("Date") & ">Click Me</A>" %>

That SHOULD generate something like the following HTML code for each entry:

<a href=bydate.asp?action=SetDate=12-16-99>Click Me</a>

You may run into issues with "s (since there are none) and encoding the date, depending on it's format in the database.

Is this what you are trying to do?

Tom
   

Avatar of Mark Franz
Tom, you are right about the "s, but more with the =Setdate=" synatax.  Try this instead;

<% astring = "<A HREF=bydate.asp?action="SetDate=' & rsCustomersList("Date") & '">Click Me</A>" %>

Mark
Mark/magoo2:

Actually, I think passing the date format is probably going to be a big old pain, probably something like:

<% astring = "<A HREF=bydate.asp?action="SetDate=' & Server.HTMLEncode(rsCustomersList("Date")) & '">Click Me</A>" %>

(if that's the right syntax)

Tom

The date format is going to wreak havok on the browsers... I know the data will display differently in other countries, but here in the good old US of A, it will appear as 12/17/99  In Perl you can escape the slashes, I don't know if HTMLEncode will pass the date string correctly.  

One way to find out!  :-)
Try this;

<%
SetDate = Server.HTMLEncode(Date())
%>
<A HREF="bydate.asp?action=<%= SetDate %>">Click Me</A>

Mark
As I though the HTMLEncode failed to encode the / properly, so you may want to just delete the / entirely;

<%@ language=VBScript%>

<%
SetDate = Server.HTMLEncode(Date())
SetDate = Replace(SetDate, "/" ,"")
%>
<A HREF="bydate.asp?action=<%= SetDate %>">Click Me</A>
You want to use URLEncode not HTMLEncode to pass the date.

URLEncode replaces characters that cause problems in URLs (spaces, symbols, etc).

HTMLEncode replaces text with HTML for symbols that give HTML problems (caret signs, apostrophes)

<a href="bydate.asp?action=<%=server.urlencode(date)%>">Click Me</a>

Good catch, cw.

Tom
The HTMLEncode URLEndode syntax beat me up last week for a good hour... data storage and retrieval... In fact, I ended up using a suggestion by Tom instead;

<%
tText = objRS.Fields("text2")
tText = replace(replace(tText,vbCRLF,"<BR>"),"  ","&nbsp;&nbsp;")
Response.Write tText
%>

( I only needed to catch spaces and <cr>)
Avatar of magoo2

ASKER

Thanks for all the comments

 The code I pasted earlier doesnt have to stay the way it is (or even close). I just want to pass the value of the date field to the next page the easiest way possible.

It will be using the value of the date field in this SQL query on the next page.

SQLQuery = "SELECT * FROM Customers WHERE Date = #" & setDate & "# ORDER BY LastName"

 Is there a painless way to do that?

Many thanks

Magoo
ASKER CERTIFIED SOLUTION
Avatar of clockwatcher
clockwatcher

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
<a href="mysearchpage.asp?mydate=<%=server.urlencode("Date")%>">Look up <% Date %></a>

' MySearchPage.asp

<%
Dim myDate, Action

myDate = request.querystring("mydate")

sqlquery = "select * from customers where datefield = #" & myDate & "# order by lastname"
 
 
 


Avatar of magoo2

ASKER

Thank you for the comments. I am accepting clockwatchers comments for the answer (it worked first) , but would like to give you other guys some points for all the info youve given.

One more question...this bit below works great for passing one variable(?) , how would I alter it to pass two variables (Date and Time).

<a href="bydate.asp?mydate=<%=server.urlencode(rsCustomersList("Date"))%>"><%= rsCustomersList("Date")%></a>


Thanks for all the help :)

Magoo
<a href="bydate.asp?mydate=<%=server.urlencode(rsCustomersList("Date"))%>&myTime=<%rsCustomerList(Time")%>"><%= rsCustomersList("Date")%></a>

Mark
I just thought of something... if you are going to do a query on a date field in a dB, what is the search going to find if the look-up string is like this;

myDate = 12%2F17%2F99

How is the dB going to handle the ascii characters on a query?

In other words, the date is going to pass this value to the SQL query as it is, unless you re-process it, or use my suggestion and use Replace()

Mark
Mark,

Request.Querystring automatically handles the conversion back to normal.  Otherwise, you'd end up with junk when handling your forms on a GET request.

If you actually wanted to use the encoded version of the string, you'd have to use request.servervariables("QUERY_STRING") and parse it yourself.  That's what the request.querystring object does for you.  It parses the querystring into a collection of name/value pairs that are url-DEcoded.
OK... you've lost me.  Obviously this would only work if the query was going to be on 1/1/2000;

<a href="mysearchpage.asp?mydate=<%=server.urlencode("1/1/2000")%>">Look up 1/1/2000</a>

So, we use this instead;

<a href="mysearchpage.asp?mydate=<%=server.urlencode("Date")%>">Look up <% Date %></a>

Whereas when we pass 'mydate' to mysearchpage.asp it is interpreted as either 12%2F17%2F99 or 12/17/99.  Using Request.QueryString() with Get on a form submit would decode the 12%2F17%2F99 string to 12/17/99?

I just don't see where this will work...  If you only want to pass the date as a string var than you could just use this; (without any encoding)

<form method="get">
<A HREF="bydate.asp?action=<%= Date() %>">Click Me</A>
</form>

This works fine in a simple world of browsers because we all know that the / is interpreted fine.

Mark
Mark,

I think you're completely missing the difference between URLEncode and HTMLEncode.  

An URL is the request.  So '/' isn't fine in an url querystring--- it's a directory separator (e.g., http://www.somewhere.com/directory/a.html?myvar=/what/ever/ is not fine-- some browsers may take it, but some may not).  The reason URLEncode alters the '/' symbol is because that it IS NOT OKAY in an URL.  That's like saying '?' is fine because browsers simply write out a ?.  A '?' is fine in HTML, but it's definitely not fine in an URL.  It demarcates the beginning of the querystring.

That's why there are two functions-- URLEncode and HTMLEncode.  URLEncode makes sure that the URL doesn't contain any characters that may mess with it.  The space is a perfect example of a character that has to be url-encoded.  The following will work fine in Internet Explorer, but won't work in Netscape-- you'll get a malformed URL request.

http://www.somewhere.com/mypage.asp?v1=hello there&v2=whatever

So the safest way to handle a querystring in ALL CASES is to URL encode it.

On the other end, when you retrieve the variables passed within a querystring-- Request.Querystring automatically takes care of unencoding it for you.

Try this simple test-- from your own post above.

bydate.asp
-----------
<html>
<body>
The complete untouched/unparsed querystring was: <%=request.servervariables("QUERY_STRING")%><BR><BR>
But, Request.querystring parses it and gives me this as the action variable: <%=request.querystring("action")%>
</body>
</html>

And call it with the following:

http://www.wherever.com/bydate.asp?action=12%2F17%2F99

The request.querystring will parse the querystring, unencode it and produce 12/17/99.  It's the same thing that happens when you use GET.  The browser's url-encode everything behind the scenes and then pass it to the server.   Try the following:

anothertest.asp
---------------
<html>
<body>
The complete untouched querystring was: <%=request.servervariables("QUERY_STRING")%><BR><BR>
But, Request.querystring parses it and gives me this as the action variable: <%=request.querystring("action")%>
<br><br>
<form method="GET" action="anothertest.asp">
<input name="action" value="12/17/99">
<input type="submit">
</form>
</body>
</html>

Press the submit button, it will GET itself and you'll notice that the browser has url-encoded the 12/17/99 behind the scenes for you-- because the "/" isn't okay in an URL.


----------------------------------------
HTMLEncode is a completely different function and has nothing to do with URLs.  HTMLEncode deals with encoding characters that are special to HTML and may cause the HTML parser to choke.  You're right it wouldn't touch the '/' or the '?', because those don't have any special meaning within HTML.  Its main use is to replace the caret symbols '<' and '>', but it also replaces a few more symbols that might choke the browser.
cw:

Good explanation.  Thanks!

Tom
Thanks CW, you are a great professor... ;-)  I understand much more clearly now the concepts of URL vs. HTML Encode and it's use with request.querystring().

Thanks,

Mark