Click to See Complete Forum and Search --> : transfer value between webpages


zhang11
06-02-2003, 01:09 PM
Dear all,

I used some copied Javascripts which to transfer value from the first webpage to the second webpage. However when the second page open, all the value areas apeard as "undefined". Please help if you know how to solve the problem. The code is followed.


Thanks.


The first webpage code:

<HEAD>

</head>
<BODY>

<center>
<form type=get action="2.html">
<table border=1>
<tr>
<td>First Name:</td>
<td><input type=text name=firstname size=10></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type=text name=lastname size=10></td>
</tr>
<tr>
<td>Age:</td>
<td><input type=text name=age size=3></td>
</tr>
<tr>
<td colspan=2><input type=submit value="Submit!">
</td>
</tr>
</table>
</form>
</center>

The second webpage code:


<HEAD>

<SCRIPT LANGUAGE="JavaScript">

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function getParams() {
var idx = document.URL.indexOf('?');
var params = new Array();
if (idx != -1) {
var pairs = document.URL.substring(idx+1, document.URL.length).split('&');
for (var i=0; i<pairs.length; i++) {
nameVal = pairs[i].split('=');
params[nameVal[0]] = nameVal[1];
}
}
return params;
}
params = getParams();
// End -->
</script>
</HEAD>

<BODY>

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin

var firstname = unescape(params["firstname"]);
var lastname = unescape(params["lastname"]);
var age = unescape(params["age"]);

document.write("firstname = " + firstname + "<br>");
document.write("lastname = " + lastname + "<br>");
document.write("age = " + age + "<br>");
// End -->
</script>

scriptkid
06-02-2003, 01:13 PM
<form type=get action="2.html">

Change the form tag to look like the following first of all and then see how it works

<form method="get" action="2.html">

Khalid Ali
06-02-2003, 01:21 PM
you must be looking for some thing like this ..:-)

DemoForwardFormDataViaURL.html (http://www.webapplikations.com/pages/html_js/forms/DemoForwardFormDataViaURL.html)

scriptkid
06-02-2003, 01:26 PM
<script language="javascript">
function QueryString(key)
{
var value = null;
for (var i=0;i<QueryString.keys.length;i++)
{
if (QueryString.keys[i]==key)
{
value = QueryString.values[i];
break;
}
}
return value;
}
QueryString.keys = new Array();
QueryString.values = new Array();

function QueryString_Parse()
{
var query = window.location.search.substring(1);
var pairs = query.split("&");

for (var i=0;i<pairs.length;i++)
{
var pos = pairs[i].indexOf('=');
if (pos >= 0)
{
var argname = pairs[i].substring(0,pos);
var value = pairs[i].substring(pos+1);
QueryString.keys[QueryString.keys.length] = argname;
QueryString.values[QueryString.values.length] = value;
}
}

}

QueryString_Parse();

//usage: QueryString(string key)
//example:
//url is: http://www.google.com?search=hello
//QueryString("search") returns hello
//case sensitive
</script>

zhang11
06-02-2003, 06:06 PM
It's still not working.

zhang11
06-02-2003, 06:31 PM
Thank you all. Especially scriptkid.

Khalid Ali
06-03-2003, 08:32 AM
Originally posted by zhang11
It's still not working.

Well if you had tried any of the suggestions above it would have worked..

icupn
04-09-2004, 12:46 AM
This script works perfectly. I am using on one of my webpages. YOu have to have 2 pages. Your are missing the entire first page. The first page has the form with the input lines that are named firstname lastname etc... you need that form. If you use a different form you have to change the firstname lastname to the name of the input values of the form you are using. This script is just a basis of how to write your own. I totally manipulated this script to do exactly what I want it to do. Hope this info helps.