Gary Williams
10-02-2002, 11:20 PM
Transfering data from Popup Form to Original Form
Using the following two forms, I can transfer text and dropdown list data back and forth OK but I can't manage to write the radio buttons or checkboxes values set in the popup form, back into the main form.
-----------------------
THE SCRIPTS
Here is how to call up a Second Enquiry Form (in a pop up window) so you can ask additional questions when necessary, the answers entered on the Popup Enquiry Form being automatically transfered back to the Main Enquiry Form for submission, when the popup is closed.
Although I have left the text fields on the main form visible (for testing purposes), you would usually have these as hidden fields to store the popup forms' data values after the popup window has been closed.
This system keeps your Main Enquiry Form as short as possible but allows additional questions to be asked only when necessary. If you have a question like "Are you the only person travelling, or are there any others?" and you have a pair of radio buttons for the answers - First one "No - only me" and the Second one "Yes - others are travelling with me", then use the second radio button (when clicked) to call the popup form using:
onclick="window.open('popup. htm','popup','width=400,height=250,left=350,top=20
0')">
to ask the additional questions about the other travellers.
----------------------------------
Create two files, 'popup.htm' and 'form.htm', from the code below:
----------------------------------
<!-- This is form.htm -->
<HTML>
<HEAD>
<TITLE>Form and additional Popup Form</TITLE>
</HEAD>
<BODY>
<FORM NAME="form" ACTION="" METHOD=POST>
<P><INPUT TYPE=BUTTON NAME="Pop Up Button" VALUE="Pop Up Window" onclick="window.open('popup. htm','popup','width=400,height=450,left=350,top=20
0')"></P>
<P><INPUT TYPE=TEXT NAME="firstname2" VALUE="" SIZE=23 MAXLENGTH=23>firstname2</P>
<P><INPUT TYPE=TEXT NAME="secondname2" VALUE="" SIZE=23 MAXLENGTH=23>secondname2</P>
<P><INPUT TYPE=TEXT NAME="lastname2" VALUE="" SIZE=23 MAXLENGTH=23>lastname2</P>
<P><SELECT NAME="title2">
<OPTION VALUE="" SELECTED>Please Select</OPTION>
<OPTION VALUE="">----------------------------------</OPTION>
<OPTION VALUE="Executors">Executors</OPTION>
<OPTION VALUE="Dr">Doctor</OPTION>
<OPTION VALUE="Mr">Mr</OPTION>
<OPTION VALUE="Mrs">Mrs</OPTION>
<OPTION VALUE="Miss">Miss</OPTION>
<OPTION VALUE="Ms">Ms</OPTION>
</SELECT>
</P>
<INPUT TYPE=RADIO NAME="radiobutton" VALUE="No">No
<INPUT TYPE=RADIO NAME="radiobutton" VALUE="Yes">Yes
<P><INPUT TYPE=TEXT NAME="radiobuttonvalue" VALUE="" SIZE=23 MAXLENGTH=23>radiobutton value</P>
<P>
<INPUT TYPE=CHECKBOX NAME="checkbox" VALUE="1">
<P><INPUT TYPE=TEXT NAME="checkboxvalue" VALUE="" SIZE=23 MAXLENGTH=23>checkbox value</P>
</P>
</FORM>
</BODY>
</HTML>
-----------------------------------------------------
-----------------------------------------------------
<!-- This is popup.htm -->
<HTML>
<HEAD>
<TITLE>Popup Form - for additional questions</TITLE>
<SCRIPT LANGUAGE=JAVASCRIPT>
function validbutton() {
// make sure they select a radio button
button = -1
for (i=0; i<popup.radiobutton.length; i++) {
if (popup.radiobutton[i].checked)
button = i
}
if (button == -1) {
alert("You didn't choose a radio button")
return false
}
else if (button == 0) {
opener.document.form.radiobuttonvalue.value="No";
return false
}
else if (button == 1) {
opener.document.form.radiobuttonvalue.value="Yes";
return false
}
}
function validbox() {
// make sure they select the checkbox
if (popup.checkbox.checked) {
opener.document.form.checkboxvalue.value="Checked";
return false
}
else {
alert("You didn't select the checkbox")
return false
}
}
function transferdataout() {
opener.document.form.title2.value=document.popup.title2.value;
opener.document.form.firstname2.value=document.popup.firstname2.value; opener.document.form.secondname2.value=document.popup.secondname2.value;
opener.document.form.lastname2.value=document.popup.lastname2.value;
return validbutton(this);
return validbox(this);
self.close();
return false
}
function loaddatain() {
document.popup.title2.value=opener.document.form.title2.value;
document.popup.firstname2.value=opener.document.form.firstname2.value;
document.popup.secondname2.value=opener.document.form.secondname2.value;
document.popup.lastname2.value=opener.document.form.lastname2.value;
return false
}
</SCRIPT>
</HEAD>
<BODY onload="loaddatain(this);">
<FORM NAME="popup" ACTION="" METHOD=POST>
<P><INPUT TYPE=TEXT NAME="firstname2" VALUE="" SIZE=20 MAXLENGTH=50>First Name/Initial</P>
<P><INPUT TYPE=TEXT NAME="secondname2" VALUE="" SIZE=20 MAXLENGTH=50>Second Name/Initial</P>
<P><INPUT TYPE=TEXT NAME="lastname2" VALUE="" SIZE=20 MAXLENGTH=50>Last Name</P>
<P><SELECT NAME="title2">
<OPTION VALUE="" SELECTED>Please Select</OPTION>
<OPTION VALUE="">----------------------------------</OPTION>
<OPTION VALUE="Executors">Executors</OPTION>
<OPTION VALUE="Dr">Doctor</OPTION>
<OPTION VALUE="Mr">Mr</OPTION>
<OPTION VALUE="Mrs">Mrs</OPTION>
<OPTION VALUE="Miss">Miss</OPTION>
</SELECT>
</P>
<P>
<INPUT TYPE=RADIO NAME="radiobutton" VALUE="No">No
<INPUT TYPE=RADIO NAME="radiobutton" VALUE="Yes">Yes
</P>
<P>
<INPUT TYPE=BUTTON NAME="testradiobutton" VALUE="Test Radio Button" onClick="return validbutton(this);">
</P>
<P>
<INPUT TYPE=CHECKBOX NAME="checkbox" VALUE="1">
</P>
<P>
<INPUT TYPE=BUTTON NAME="testcheckbox" VALUE="Test Checkbox" onClick="return validbox(this);">
</P>
<INPUT TYPE=BUTTON NAME="Transfer Data" VALUE="Transfer Data and Close Window"
onclick="return transferdataout(this);">
<P>
<INPUT TYPE=BUTTON NAME="Close Window" VALUE="Close Window" onclick="self.close();">
</P>
</FORM>
</BODY>
</HTML>
Using the following two forms, I can transfer text and dropdown list data back and forth OK but I can't manage to write the radio buttons or checkboxes values set in the popup form, back into the main form.
-----------------------
THE SCRIPTS
Here is how to call up a Second Enquiry Form (in a pop up window) so you can ask additional questions when necessary, the answers entered on the Popup Enquiry Form being automatically transfered back to the Main Enquiry Form for submission, when the popup is closed.
Although I have left the text fields on the main form visible (for testing purposes), you would usually have these as hidden fields to store the popup forms' data values after the popup window has been closed.
This system keeps your Main Enquiry Form as short as possible but allows additional questions to be asked only when necessary. If you have a question like "Are you the only person travelling, or are there any others?" and you have a pair of radio buttons for the answers - First one "No - only me" and the Second one "Yes - others are travelling with me", then use the second radio button (when clicked) to call the popup form using:
onclick="window.open('popup. htm','popup','width=400,height=250,left=350,top=20
0')">
to ask the additional questions about the other travellers.
----------------------------------
Create two files, 'popup.htm' and 'form.htm', from the code below:
----------------------------------
<!-- This is form.htm -->
<HTML>
<HEAD>
<TITLE>Form and additional Popup Form</TITLE>
</HEAD>
<BODY>
<FORM NAME="form" ACTION="" METHOD=POST>
<P><INPUT TYPE=BUTTON NAME="Pop Up Button" VALUE="Pop Up Window" onclick="window.open('popup. htm','popup','width=400,height=450,left=350,top=20
0')"></P>
<P><INPUT TYPE=TEXT NAME="firstname2" VALUE="" SIZE=23 MAXLENGTH=23>firstname2</P>
<P><INPUT TYPE=TEXT NAME="secondname2" VALUE="" SIZE=23 MAXLENGTH=23>secondname2</P>
<P><INPUT TYPE=TEXT NAME="lastname2" VALUE="" SIZE=23 MAXLENGTH=23>lastname2</P>
<P><SELECT NAME="title2">
<OPTION VALUE="" SELECTED>Please Select</OPTION>
<OPTION VALUE="">----------------------------------</OPTION>
<OPTION VALUE="Executors">Executors</OPTION>
<OPTION VALUE="Dr">Doctor</OPTION>
<OPTION VALUE="Mr">Mr</OPTION>
<OPTION VALUE="Mrs">Mrs</OPTION>
<OPTION VALUE="Miss">Miss</OPTION>
<OPTION VALUE="Ms">Ms</OPTION>
</SELECT>
</P>
<INPUT TYPE=RADIO NAME="radiobutton" VALUE="No">No
<INPUT TYPE=RADIO NAME="radiobutton" VALUE="Yes">Yes
<P><INPUT TYPE=TEXT NAME="radiobuttonvalue" VALUE="" SIZE=23 MAXLENGTH=23>radiobutton value</P>
<P>
<INPUT TYPE=CHECKBOX NAME="checkbox" VALUE="1">
<P><INPUT TYPE=TEXT NAME="checkboxvalue" VALUE="" SIZE=23 MAXLENGTH=23>checkbox value</P>
</P>
</FORM>
</BODY>
</HTML>
-----------------------------------------------------
-----------------------------------------------------
<!-- This is popup.htm -->
<HTML>
<HEAD>
<TITLE>Popup Form - for additional questions</TITLE>
<SCRIPT LANGUAGE=JAVASCRIPT>
function validbutton() {
// make sure they select a radio button
button = -1
for (i=0; i<popup.radiobutton.length; i++) {
if (popup.radiobutton[i].checked)
button = i
}
if (button == -1) {
alert("You didn't choose a radio button")
return false
}
else if (button == 0) {
opener.document.form.radiobuttonvalue.value="No";
return false
}
else if (button == 1) {
opener.document.form.radiobuttonvalue.value="Yes";
return false
}
}
function validbox() {
// make sure they select the checkbox
if (popup.checkbox.checked) {
opener.document.form.checkboxvalue.value="Checked";
return false
}
else {
alert("You didn't select the checkbox")
return false
}
}
function transferdataout() {
opener.document.form.title2.value=document.popup.title2.value;
opener.document.form.firstname2.value=document.popup.firstname2.value; opener.document.form.secondname2.value=document.popup.secondname2.value;
opener.document.form.lastname2.value=document.popup.lastname2.value;
return validbutton(this);
return validbox(this);
self.close();
return false
}
function loaddatain() {
document.popup.title2.value=opener.document.form.title2.value;
document.popup.firstname2.value=opener.document.form.firstname2.value;
document.popup.secondname2.value=opener.document.form.secondname2.value;
document.popup.lastname2.value=opener.document.form.lastname2.value;
return false
}
</SCRIPT>
</HEAD>
<BODY onload="loaddatain(this);">
<FORM NAME="popup" ACTION="" METHOD=POST>
<P><INPUT TYPE=TEXT NAME="firstname2" VALUE="" SIZE=20 MAXLENGTH=50>First Name/Initial</P>
<P><INPUT TYPE=TEXT NAME="secondname2" VALUE="" SIZE=20 MAXLENGTH=50>Second Name/Initial</P>
<P><INPUT TYPE=TEXT NAME="lastname2" VALUE="" SIZE=20 MAXLENGTH=50>Last Name</P>
<P><SELECT NAME="title2">
<OPTION VALUE="" SELECTED>Please Select</OPTION>
<OPTION VALUE="">----------------------------------</OPTION>
<OPTION VALUE="Executors">Executors</OPTION>
<OPTION VALUE="Dr">Doctor</OPTION>
<OPTION VALUE="Mr">Mr</OPTION>
<OPTION VALUE="Mrs">Mrs</OPTION>
<OPTION VALUE="Miss">Miss</OPTION>
</SELECT>
</P>
<P>
<INPUT TYPE=RADIO NAME="radiobutton" VALUE="No">No
<INPUT TYPE=RADIO NAME="radiobutton" VALUE="Yes">Yes
</P>
<P>
<INPUT TYPE=BUTTON NAME="testradiobutton" VALUE="Test Radio Button" onClick="return validbutton(this);">
</P>
<P>
<INPUT TYPE=CHECKBOX NAME="checkbox" VALUE="1">
</P>
<P>
<INPUT TYPE=BUTTON NAME="testcheckbox" VALUE="Test Checkbox" onClick="return validbox(this);">
</P>
<INPUT TYPE=BUTTON NAME="Transfer Data" VALUE="Transfer Data and Close Window"
onclick="return transferdataout(this);">
<P>
<INPUT TYPE=BUTTON NAME="Close Window" VALUE="Close Window" onclick="self.close();">
</P>
</FORM>
</BODY>
</HTML>