JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 15th, 2002, 09:55 AM
Robert12345 Robert12345 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2000
Posts: 65 Robert12345 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 31 m 38 sec
Reputation Power: 9
Question Reading URL passed variables in JavaScript

I'm passing 4 variables from a Form via the GET method along the URL to a new page. That's the easy bit.

I can't understand the Javascript examples/tutorials online that try to explain how to read these in the newpage.

I don't really need to display the results on the new page, just have them inserted into a hidden field in a Form so that they can be POSTed along with extra information added to a 2nd Form on this new page.

I would be very grateful for some assistance.

Reply With Quote
  #2  
Old August 15th, 2002, 10:17 AM
piglet's Avatar
piglet piglet is offline
Jack of all trades
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 14 piglet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to piglet
Hi Robert,

Thisis pretty simple - you just need to split the search string until you've got the bits in a state you can do something with them.

Try this with test.html?foo=bar;blah=whatever address:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE> New Document </TITLE></HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
if (location.search != "")
{
	var x = location.search.substr(1).split(";")
	for (var i=0; i<x.length; i++)
	{
		var y = x[i].split("=");
		alert("Key '" + y[0] + "' has the content '" + y[1]+"'")
	}
}	
//-->
</SCRIPT>
</BODY>
</HTML>

Reply With Quote
  #3  
Old August 16th, 2002, 04:13 PM
Robert12345 Robert12345 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2000
Posts: 65 Robert12345 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 31 m 38 sec
Reputation Power: 9
Dear Piglet

I thank you for your speedy reponse ...I hope you'll forgive me if I say that I don't follow your answer. Let me split my question down into 2 parts.

1. Lets say my Form generates a URL of:
http://www.mydomain.com?Submit=var1...ar4=a_selection

I understand the basic principles of what the Javascript should do - strip out &s, =signs, etc. I need help with a script that will deal with the general format I have outlined above.

Notes: although the stripping out of spaces, etc will not be required since all the field names and selection choices will be pre-written, I appreciate that including them in an example will be beneficial to others reading this.

2. How does one then take one of the stripped out variables and insert it into the likes of the value field in a second form as indicated in the ??? below

<input type="hidden" name="var1" value="???">

Can you help?

Reply With Quote
  #4  
Old August 16th, 2002, 04:58 PM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 9
Send a message via AIM to bricker42
There's a good script for this from webmokey.com

http://hotwired.lycos.com/webmonkey...gory=forms_data

Reply With Quote
  #5  
Old August 16th, 2002, 05:45 PM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 sec
Reputation Power: 9
Here's some quick code I made, and I kept it as uncompact as possible to keep it clear, as well as used generous commenting.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Testing the Foo</TITLE>
</HEAD>
<BODY>
<!--
    Our input variable. It will eventually contain the value
    passed to the 'foo' variable via GET.
-->
<FORM name="MyForm">
    <INPUT name="foo">
</FORM>
<SCRIPT LANGUAGE="JavaScript">
<!--
// Create a global array that will hold the value of each variable,
// keyed by the name of the variable.
var GETDATA = new Array();

// Get the string that follows the "?" in the window's location.
var sGet = window.location.search;
if (sGet) // if has a value...
{
    // Drop the leading "?"
    sGet = sGet.substr(1);
    
    // Generate a string array of the name value pairs.
    // Each array element will have the form "foo=bar"
    var sNVPairs = sGet.split("&");
    
    // Now, for each name-value pair, we need to extract
    // the name and value.
    for (var i = 0; i < sNVPairs.length; i++)
    {
        // So, sNVPairs[i] contains the current element...
        // Split it at the equals sign.
        var sNV = sNVPairs[i].split("=");
        
        // Assign the pair to the GETDATA array.
        var sName = sNV[0];
        var sValue = sNV[1];
        GETDATA[sName] = sValue;
    }
}

// Finally, assign the value to foo.
if (GETDATA["foo"] != undefined)
    // Just in case we forgot to pass something to foo...
    document.forms.MyForm.foo.value = GETDATA["foo"];
//-->
</SCRIPT>
</BODY>
</HTML>


Hope this makes it clear. I used it by first saving it to a temp place on my hard drive. Double clicking opened it, then all I did was changed the url to add a "?foo=bar".

-mj

Reply With Quote
  #6  
Old August 17th, 2002, 06:30 AM
Robert12345 Robert12345 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2000
Posts: 65 Robert12345 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 31 m 38 sec
Reputation Power: 9
I understand!!!!

Many thanks to Bricker and MJ ...I read over the link info you posted Bricker and it looked well annotated ...but MJs looked ever more readable...and Hey Presto! I understood it!

For those like myself who can be a bit slow in working out certain things, I post the following demo that made me feel confident that I had what I was looking for, viz a script capable of reading multiple field values and then displaying them in a second Form (although I may well eventually make them hidden rather than text). I thank you all for your time and effort and hope my simplistic interpretation below will help a few others even more.

The Posting Form saved as 1.htm with another 4 fields added:
<html>
<body>
<form name=myForm method="GET" action="2.htm">
<input type=text name=foo size="20">
<input type=text name=goo size="20">
<input type=text name=hoo size="20">
<input type=text name=joo size="20">
<input type=text name=koo size="20">
<input type=submit>
</form>
</body>
</html>

The receiving Form (MJ's script from above) saved as 2.htm with the following changes:
1. Matching 'target' fields in the page 2.htm
<FORM name="MyForm">
<INPUT name="foo"><br>
<INPUT name="goo"><br>
<INPUT name="hoo">
<INPUT name="joo"><br>
<INPUT name="koo"><br>
</FORM>
....
2. Matching Javascript near the bottom of the script to extract matching data from the array...changes made in the middle and at the end of each line:
document.forms.MyForm.foo.value = GETDATA["foo"];
document.forms.MyForm.goo.value = GETDATA["goo"];
document.forms.MyForm.hoo.value = GETDATA["hoo"];
document.forms.MyForm.joo.value = GETDATA["joo"];
document.forms.MyForm.koo.value = GETDATA["koo"];

Wishing to hide a field would involve editing to:
<input type=hidden name=foo size="20">

This is probably a very crude editing ..but it works...many thanks to all

Reply With Quote
  #7  
Old August 17th, 2002, 03:21 PM
piglet's Avatar
piglet piglet is offline
Jack of all trades
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 14 piglet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to piglet
Hi

Your original question was to populate hidden fields.

The easiest thing to do is to write them directly into the form...

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE> New Document </TITLE></HEAD>
<BODY>

<form method="post" action="blah.asp">
<SCRIPT LANGUAGE="JavaScript">
<!--
if (location.search != "")
{
	var x = location.search.substr(1).split(";")
	for (var i=0; i<x.length; i++)
	{
		var y = x[i].split("=");
		document.writeln('<input type="hidden" name="'+y[0] + '" value="' + y[1]+'">')
	}
}	

//-->
</SCRIPT>
<input type="submit">
</form>
</BODY>
</HTML>

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Reading URL passed variables in JavaScript


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT