/    Sign up×
Community /Pin to ProfileBookmark

Transfering variables across pages

I read the article about transferring variables across pages, and the author stated that his technique only allowed two variables to be transferred. I had a couple of ideas that might work, but I need a more experienced programmer to tell me if there is something that will prevent this from working: (1)The computer could set the URL to a variable (or in an invisible textbox) and index the last = sign, take the info, then ‘chop off’ everything after the last & sign, and so on until it sees the ? sign, which would indicate that the variables had ended. (2) The second way would be this: On the first page, when the user types his or her information, the computer would set a variable as follows–> If the user types “Andrew” the variable would be “@Andrew” then on the next box, if the user types “Saucetier” the variable would be “#Saucetier”. This way, the computer could index the symbols (@ and # etcetera) and find the values that follow. I haven’t actually begun to do these things yet, so I’ll post the code once I’m done, to clarify. I will probably need HELP! Hopefully, from this or the code I’ll post, you’ll understand what I’m trying to do! 😮

to post a comment
JavaScript

14 Comments(s)

Copy linkTweet thisAlerts:
@hyperliskApr 03.2006 — Yeah, you could pass them through the URL, like a form with method="get" and then write a function to extract each individual name=value pair.
Copy linkTweet thisAlerts:
@stalepretzel59authorApr 05.2006 — Which would be the easiest way? add an @, #, or $; chop off the last part each time; or finding the name and grabbing the value from after it? Is there any easier way? Is any way easy? With JS there are always a thousaand ways to do things, And I think we could thing of a ton
Copy linkTweet thisAlerts:
@hyperliskApr 05.2006 — Here is the way I do it...
<i>
</i>function GETvars(toGet){

address = document.location.href;
if(address.indexOf("?") == -1) return undefined;
address = address.split("?")[1];
address = address.split("&amp;");
var theVars = new Array();
for(i=0;i&lt;address.length;i++){
key = address[i].split("=")[0];
theVars[key] = address[i].split("=")[1];
}
specialChars = new Array(' ',':',';','\?','@','&amp;','=','\$',',','#','\+','\/');
entities = new Array('\+','%3A','%3B','%3F','%40','%26','%3D','%24','%2C','%23','%2B','%2F');
if(!theVars[toGet]) return undefined;
undecoded = theVars[toGet];
var re;
if(typeof decodeURI == 'function') {
undecoded = decodeURI(undecoded);
for(var i=0;i&lt;entities.length;i++) {
re = new RegExp(entities[i],'ig');
undecoded = undecoded.replace(re,specialChars[i]);
}
} else {
undecoded = unescape(undecoded);
}
return undecoded;

}

Then, say you have this URL:
<i>
</i>http://www.yahoo.com?var1=hello&amp;var2=goodbye

To get the value of var1, all you do is call the function like this:
<i>
</i>var1 = GETvars("var1");

Super-easy!
Copy linkTweet thisAlerts:
@stalepretzel59authorApr 14.2006 — Super Easy?

Some of that stuff is super not-easy, for me at least. I'll google all of those phrases, but I was thinking of doing something really simple first. I designed the first part, kind of. I already ran into problems. Right now I'm just trying to make the page display "Thank you for submiting your information" and then simply display everything after the "?" in the URL.

If the URL is

something://xxx.hello.zzz/whoadudehtml?var1=hello&var2=hi

it would display
[CODE]Thank you for submitting your information. var1=hello&var2=hi[/CODE]
I know it's not something that is useful, but I want to be able to find problems right away and fix them right away. Well, I found the problem, I just can't figure out what the problem is.

Here is the first part:
[CODE]
<html>
<head>
<title>Forms R Us</title>
</head>
<body>
<body bgcolor="#99CC99">
<font color="#115577" size="4">
Thank you for entering your information.


<script type="text/javascript">
var loc= window.location
var f=loc.indexOf("?")+1
var l=loc.length
var xxx= loc.substring(f,l)
document.write(xxx)
</script>


</body>
</html>
[/CODE]




THANKS FOR YOUR HELP
Copy linkTweet thisAlerts:
@balloonbuffoonApr 14.2006 — [I]location.search[/I] will give you the "?" and everything following that. (Or nothing if there is no query string.) Then you can chop off the first character. [CODE]if (location.search!="") {
var query = location.search.substring(1);
}[/CODE]


--Steve
Copy linkTweet thisAlerts:
@stalepretzel59authorApr 14.2006 — What's the one mean?
Copy linkTweet thisAlerts:
@hyperliskApr 14.2006 — I knew there was a way to get that! But I just couldn't remember, so I did it a different way.

stalepretzel, you could write your code like this:
<i>
</i>&lt;script type="text/javascript"&gt;
var loc= window.location.[color=red]href[/color];
var f=loc.indexOf("?")+1;
var xxx= loc.[color=red]substr(f)[/color];
document.write(xxx);
&lt;/script&gt;

The changes are in red. What substr() does is it will act like substring(), but it starts at the first parameter, and continues for a given length, if the length is added on as the second parameter. Because I left off the second parameter, then it will return the part of the string from f all the way to the end of the string. I hope that made sense...
Copy linkTweet thisAlerts:
@balloonbuffoonApr 14.2006 — What's the one mean?[/QUOTE]
The one means to offset the string by 1 character, or in other words, chop off the first character.
What substr() does is it will act like substring(), but it starts at the first parameter, and continues for a given length, if the length is added on as the second parameter. Because I left off the second parameter, then it will return the part of the string from f all the way to the end of the string.[/QUOTE]
substring() will act the same way if you don't put a second parameter.

--Steve
Copy linkTweet thisAlerts:
@hyperliskApr 14.2006 — Hmm... i guess it does... Oh well, I am just so used to always using substr()...
Copy linkTweet thisAlerts:
@balloonbuffoonApr 14.2006 — Yeah, substr() is easier to use most of the time.

--Steve
Copy linkTweet thisAlerts:
@stalepretzel59authorApr 15.2006 — OK, stupid person question:

does that mean substr() is the same as substring()?

Or do I just need to go back to W3schools tutorials for the remainder of my childhood?
Copy linkTweet thisAlerts:
@hyperliskApr 16.2006 — substring([i]indexA[/i],[i]indexB[/i]) returns the part of the string that is between indexA and indexB. substr([i]offset[/i][,[i]length[/i]]) returns the part of the string that starts at offset and continues to the specified length, or the end of the string.
Copy linkTweet thisAlerts:
@stalepretzel59authorApr 19.2006 — Thanks, I'm gonna try it out soon.
Copy linkTweet thisAlerts:
@cjvmanJul 18.2006 — Hi all, I'm having a problem after the variable is carried. Basically I have about 20 arrays in the page the variable is carried to, I now want to call on the array which has the same name as the variable carried. for instance:

[code=html]
mypage.htm?arraytouse=packA
[/code]


<i>
</i>var packA = new array("pack A info","pack A price","pack A postage")
var packB = new array("pack B info","pack B price","pack B postage")
var packC = new array("pack C info","pack C price","pack C postage")


I've tried using the following script:
<i>
</i>function GetParam(name)
{
var start=location.search.indexOf("?"+name+"=");
if (start&lt;0) start=location.search.indexOf("&amp;"+name+"=");
if (start&lt;0) return '';
start += name.length+2;
var end=location.search.indexOf("&amp;",start)-1;
if (end&lt;0) end=location.search.length;
var result=location.search.substring(start,end);
var result='';
for(var i=start;i&lt;=end;i++) {
var c=location.search.charAt(i);
result=result+(c=='+'?' ':c);
}
return unescape(result);
}

var product=GetParam('arraytouse')

document.write(product[2])


Which should write "pack A postage", but instead returns "Undefined"

Can anyone help please??

Thanks in advance

Chris
×

Success!

Help @stalepretzel59 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...