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

Pull data from query string, copy into form

Status
Not open for further replies.

kallywag

Technical User
Jul 6, 2004
20
US
New to js here. I'm going to be receiving a URL in this format:

....etc.

I know it's messy and all to send data this way, but it's what was decided on, so here's what I need to do with it. I want to take all the info from the query string and post it into the corresponding form fields in the document. So I need to somehow isolate the query string (maybe a global JS variable? maybe another method?) and then I was thinking it would work to somehow pull each piece of information delimited with &= and put them into a simple array (split() method?). Once I have the info pulled from the URL into an array, I thought I could simply update the field's value attribute with basic javascript like this:

form1.SubscriberID.value=1523;

Then, all the values would be automatically loaded into the correct form fields. But, since I'm a js newbie, I really dont know how to go about this. Ideas? Examples? Remember I'm new, so explaining every process might be in order. Thanks.
 

Given that the key/value pairs in the URL have no key, how on earth are you going to determine which value goes in what form field?

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
assuming your url is " this will parse the url and fill in the boxes accordingly. this is also assuming that the textboxes are named exactly as the name in the name/value pair.

Code:
<html>
<script>
function getUrl() {
url=document.location;
queryString=url.split("?");
queryX=queryString[1];
nvPairs=queryX.split("&");
nvLength=nvPairs.length;
 for (c=0;c<nvLength;c++) {
 nvpX=nvPairs[c];
 nvpX2=nvpX.split("=");
 nameX=nvpX2[0];
 valueX=nvpX2[1];
 document.getElementById(nameX).value=valueX;
 }
}
</script>
<head>
<title>Test Script</title>
</head>
<body>
type: <input type=text name=type id=x value=''><br>
first name: <input type=text name=type id=fName value=''><br>
last name<input type=text name=type id=lName value=''><br>
<script>getUrl();</script>
</body>
</html>

BillyRay brings up a great point; if you send the values without the name, as your original post suggests with the url, you'll have a harder time parsing out the info...but to resolve that issue (if it is an issue and not a typo) you would order the textboxes according to the order of the info being passed...i just realized that for some reason a ; is being thrown into the url at x=subscriberID&;fName=...just take it out when testing the script.

hope that helps.

- g
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top