Click to See Complete Forum and Search --> : Passing Values Script does not work
campinluvr
02-05-2003, 08:43 AM
Can anyone figure out why this script doesn't work? It works in this sample:
http://javascript.internet.com/forms/passing-values.html
but when I use the code on my own PC, I get the word 'undefined' for each value on the second page.
Here is the source code:
http://javascript.internet.com/forms/passing-values-source.html#source
Zach Elfers
02-05-2003, 08:47 AM
It works on the web when I clicked on the links so it is a problem with your source code. Why don't your paste the code here so we can see what is wrong?
mawood
02-05-2003, 08:50 AM
This may be because you have local. That code uses the URL string to get its info. You'll either need to upload to your website to test it or access the file via IP address. There is no url string submitted when you submit a form locally. This can be verified by noting that after you hit submit, after the ...html in your address bar, there is no '?this=that&that=this' stuff.
campinluvr
02-05-2003, 08:52 AM
-------------------------------------------------------------------
Here's the first page called senddata.html:
-------------------------------------------------------------------
<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
<!-- 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>
<!-- Demonstration -->
<center>
<form type=get action="passing_values_source.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>
</body></html>
-------------------------------------------------------------------
Here's the second page called passing_values_source.html:
-------------------------------------------------------------------
<html>
<!-- STEP TWO: Paste this code into the HEAD of your second document -->
<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>
<!-- Demonstration -->
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
firstname = unescape(params["firstname"]);
lastname = unescape(params["lastname"]);
age = unescape(params["age"]);
document.write("firstname = " + firstname + "<br>");
document.write("lastname = " + lastname + "<br>");
document.write("age = " + age + "<br>");
// End -->
</script>
</body></html>
mawood
02-05-2003, 08:58 AM
Upload it and try it.