...

View Full Version : How to pass variables from one form to another using Javascript?



umhbsoccer
01-13-2004, 10:10 PM
I am trying to creat a two-part form using client side scripting only. The form would require the user to fill out certain information on the first page before being allowed to view the other sections of the form on the second page.

I have the validation down and am trying to pass the parameters into the URL of the next page which I am doing successfully. The problem I am having is getting the URL variables to display on the next page. They are writing out as "undefined".

I have included both pages of code I am using below as one of you may be able to point out my mistake. Thanks for taking the time to help an intermediate programmer...


First page code:

<head>
<title>Room Reservation and Catering Form</title>

<SCRIPT LANGUAGE="JavaScript">

function checkrequired(which) {
var pass=true;
if (document.images) {
for (i=0;i<which.length;i++) {
var tempobj=which.elements[i];
if (tempobj.name.substring(0,8)=="required") {
if (((tempobj.type=="text"||tempobj.type=="textarea")&&
tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
tempobj.selectedIndex==0)) {
pass=false;
break;
}
}
}
}
if (!pass) {
shortFieldName=tempobj.name.substring(8,30).toUpperCase();
alert("Please make sure the "+shortFieldName+" field was properly completed.");
return false;
}
else
return true;
}

</script>

</head>

<body>

<form onSubmit="return checkrequired(this)" type=get action="RoomReservationForm2.html">

<table width="100%" align="center" valign="top" cellpadding="0" cellsapcing="0" border="0">

<tr>
<td colspan="3" align="center"><font face="arial" size="3"><b><i>Children's Medical Center Dallas</i><br>Room Reservation Form</b><br><br></font></td>
</tr>

<tr align="center">
<td align="left" width="30%"><input type="text" name="requiredlastname" value="" size="15" maxlength="25"><br><font face="arial" size="2">Last name</font></td>
<td align="left" width="30%"><input type="text" name="requiredfirstname" value="" size="15" maxlength="25"><br><font face="arial" size="2">First name</font></td>
<td align="left" width="30%"><input type="text" name="requiredphone" value="" size="15" maxlength="25"><br><font face="arial" size="2">Phone extension</font></td>
</tr>
<tr>
<td colspan="3"><input type=submit value="Submit"></td>
</tr>

</table>

</form>
</body>




Second page code:

<head>
<title>Room Reservation and Catering Form 2</title>

<SCRIPT LANGUAGE="JavaScript">

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();

</script>
</head>

<body>
<SCRIPT LANGUAGE="JavaScript">

requiredlastname = unescape(params["requiredlastname"]);
requiredfirstname = unescape(params["requiredfirstname"]);
requiredphone = unescape(params["requiredphone"]);

document.write("requiredlastname = " + requiredlastname + "<br>");
document.write("requiredfirstname = " + requiredfirstname + "<br>");
document.write("requiredphone = " + requiredphone + "<br>");

</script>
</body>

DoubleV
01-13-2004, 10:47 PM
see this tutorial (http://www.javascriptkit.com/javatutors/send1.shtml)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum