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

Passing a Javascript variable to a different page 2

Status
Not open for further replies.

evr72

MIS
Dec 8, 2009
265
US
Hello,

I have tow JavaScripts
Code:
<script language="JavaScript">

temp= window.prompt ("Please Type Your Name" , "");
if (temp=="") { window.location="[URL unfurl="true"]http://daviesintranet/rma1/redirect.asp?id=<%[/URL] = Cdbl((rsGuestbook("ID")))%>";}


else if (temp=="user1"|| temp=="user2")  { window.location="[URL unfurl="true"]http://mywebsite.com/page1.asp?id=<%[/URL] = Cdbl((rsGuestbook("ID")))%>";}

else if (temp=="user3"|| temp=="user4")  { window.location="[URL unfurl="true"]http://mywebsite.com/page2.asp?id=<%[/URL] = Cdbl((rsGuestbook("ID")))%>";} 
else if (temp=="user5"|| temp=="user6") { window.location="[URL unfurl="true"]http://mywebsite.com/page3.asp?id=<%[/URL] = Cdbl((rsGuestbook("ID")))%>";} 

</script>
this first one asks for user name and creates the temp variable

This second one takes the temp variable and iserts the text into the updatedby field
Code:
<script>
function initialize()
{
 document.forms['form'].elements['updatedby'].value = temp ;
}
</script>

this works great on the page, but what I am trying to accomplish is that when have a page with the script like above that when a user enters their name it redirects them to the proper page. I have accomplish this. what I cannot figure out is how can I pass the temp variable to the new page (page1.asp, page2.asp and page3.asp)
 
Hi

evr72 said:
how can I pass the temp variable to the new page
No way. A variable can not leave its environment ( in JavaScript's case, the document ).

However, the value stored in the variable can be passed forward. Generally you can do it in two ways :
[ul]
[li]Put it somewhere where can be reached from both documents. Either cookie or web storage.[/li]
[li]Send it as GET or POST parameter.[/li]
[/ul]
I would choose the second :
Code:
[gray]// ...[/gray]

[b]else[/b] [b]if[/b] [teal]([/teal]temp[teal]==[/teal][green][i]"user1"[/i][/green][teal]||[/teal] temp[teal]==[/teal][green][i]"user2"[/i][/green][teal])[/teal]  [teal]{[/teal] window[teal].[/teal]location[teal]=[/teal][green][i]"[URL unfurl="true"]http://mywebsite.com/page1.asp?id=<%[/URL] = Cdbl((rsGuestbook("ID")))%>[highlight]&user=[/highlight]"[/i][/green][highlight][teal]+[/teal][COLOR=darkgoldenrod]escape[/color][teal]([/teal]temp[teal])[/teal][/highlight][teal];[/teal] [teal]}[/teal]

[gray]// ...[/gray]
Then would handle the value on server-side to populate the [tt]form[/tt].


Feherke.
 
Add the temp variable to the URL.

Once in the redirected page its usual to use server side languages (PHP, ASP ...) to manipulate that data there, though it can be done in JS with a bit more coding.

Code:
else if (temp=="user1"|| temp=="user2")  { window.location="[URL unfurl="true"]http://mywebsite.com/page1.asp?id=<%[/URL] = Cdbl((rsGuestbook("ID")))%>[red]&[/red]" [red]+ "temp=" + temp[/red];}



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
so here is what I have done so far

I added

Code:
<script>
var temp=<%temp = Request.querystring ("temp") %>

function initialize()
{
 document.forms['form'].elements['credupdatedby'].value = temp ;
}
</script>

To my page1.asp and got the following in my updatedby field

Code:
function initialize(){ document.forms['form'].elements['credupdatedby'].value = temp ;}

I am trying a few more things but not working so far
 
Hi

You can not just spit the value like that in the middle of the code. You have to make sure the generated JavaScript code will be syntactically valid. So enclose it in quotes :
Code:
var temp=[highlight]'[/highlight]<%temp = Request.querystring ("temp") %>[highlight]'[/highlight]
Note that you should also escape the quote ( ' ) and backslash ( \ ) characters contained by temp, but I have no idea how is that done in ASP. Do your research.

Feherke.
 
Wait, where is credupdateby element? In the Original Page or in the one you redirected to?

Did you add the temp var to the URL string in your redirection code?

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
ahhh got it

Thank you feherke and thank you vacunita

I appreciate it!!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top