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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I pass local variables?

Status
Not open for further replies.

OhioSteve

MIS
Mar 12, 2002
1,352
0
0
US
I am part of a workgroup that is developing an .asp-based application. The decision has been made to use local variables, not session variables.

We pass variables to the next page when the user presses a button. We have alot of code like this...

<form method=&quot;POST&quot; action=&quot;xxx.asp&quot;>
<input name=&quot;x1&quot; type=&quot;hidden&quot; value=&quot;<%=x1%>&quot;>
<td><p><input type=&quot;submit&quot; value=&quot;go to next page&quot; name=&quot;B1&quot;></p></td>
</form>

Then, when xxx.asp loads, we have code like this...
x1=request.form(&quot;x1&quot;)

Unfortunately, I have a problem. I have a page that momentarily loads, does a bunch of .asp stuff, and then vanishes through a response.redirect. In this situation, I can't use the button trick to &quot;send&quot; the variables. The user never even sees the page, so he can't press a button.

How do I say, &quot;when you do the response.redirect, hand off these values...&quot;???
 
I can think of three options if you wisdh to continue not using session/cookie/etc variables.
1) Add the variables to the querystring like so:
Response.Redirect &quot;newpage.asp?x1=&quot; & Request.Form(&quot;x1&quot;)

if you do this then you next page will have to be capable of pulling the data from the Request.QueryString collection as well as the Request.Form collection. Not a big deal, just means you need to check to see which your data is in and then handle it accordingly.

2) Use Server.Transfer

Response.Redirect sends headers back to the user that causes their browser to request the next page. Server.Transfer on the other hand does a server-side redirect, which keeps your program state (ie, like Request collection) intact.

3) Use client scripting

U only mention this as a possibility, I generally don't like this solution because there are to many things that can go wrong with it. You could always write all the data to a form with hidden fields and then have the onLoad in the body tag submit the form to the next page. This way the data is still transferred via POST. The drawback is, of course, that your relying on javascript, so you have to make aure it is going to work across all browsers and platforms or someone will have an issue.


Hope this helps,
-Tarwn

[sub]01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111[/sub]
The never-completed website:
 
I am not too sure but I think Server.Transfer can only transfer application and session variables.

Bye

Qatqat




Life is what happens when you are making other plans.
 
I like the idea of storing it in the URL. I tried to implement this solution.

In the first page, I put this...

Response.Redirect &quot;nextpage.asp? variable=&quot; & Request.Form(&quot;variable&quot;)

The next page loaded and displayed this at the end of the URL...

&quot;...pagename.asp?%20variable=value&quot;

I don't know why &quot;%20&quot; appeared in the URL. I do not know if that is a problem.

In the code of the second page, I wrote...

newvariable = request.querystring(&quot;variable&quot;)
response.write newvariable

UNFORTUNATELY, the newvariable is not displaying! I know that I am close to understanding this. What do I need to improve?


 
Looks like you accidentally put a space in the querystring:
Code:
Response.Redirect &quot;nextpage.asp? variable=&quot; & Request.Form(&quot;variable&quot;)

should be

Code:
Response.Redirect &quot;nextpage.asp?variable=&quot; & Request.Form(&quot;variable&quot;)
 
%20 is a space - you're posted code has a space between the ? and the variable name. Remove the space and it should work.
 
OhioSteve,

Try a Form without a Submit button like this:
Code:
<form name=&quot;frmPassVar&quot; method=&quot;POST&quot; action=&quot;xxx.asp&quot;>
  <input name=&quot;x1&quot; type=&quot;hidden&quot; value=&quot;<%=x1%>&quot;>
</form>
<script language='JavaScript'>
  document.frmPassVar.submit();
</script>
This approach works for me.

Medic
 
Okay, I eliminated the space and that eliminated the problem. Now I have a new dilema. I actually want to pass three variables, not one. I set it up to pass two values like this:

Response.Redirect &quot;nextpage.asp?value1=&quot;&Request.Form(&quot;value1&quot;)&&quot;value2=&quot;&request.form(&quot;value2&quot;)

On the receiving end, I wrote...

x= request.querystring(&quot;value1&quot;)
response.write x
y = request.querystring(&quot;value2&quot;)
response.write y

On the second page the values displayed perfectly in my URL window! However, they did not print correctly. The first one was fine. But the second one printed as &quot;value2=[the value]&quot;. Obviously, I just want the actual value.
 
Response.Redirect &quot;nextpage.asp?value1=&quot; & Request.Form(&quot;value1&quot;)&&quot;&value2=&quot;&request.form(&quot;value2&quot;)

Just add '&' before the value2 variable. You need to add '?' before the first querystring variables, but for the subsequent ones you have to add '&' in front of them.

Try this out


Uhgar
-- Alcohol and Calculus never mix! Do not drink and derive! --
 
It works! Its alive! Its alive!

Dr Frankenstein has nothing on me ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top