We help IT Professionals succeed at work.

Popup Window to Pass Value to Parent Window On Close

brownmetals
brownmetals asked
on
4,046 Views
Last Modified: 2008-04-24
Hi there.

I'm writing an ASP application using MS Access. I have a data entry form that prompts a user for an ID value from the database. I'm providing a lookup so that the user can:

1) click a lookup link - pops up new window that allows search
2) search by name and obtain the corresponding ID
3) click the ID to close the lookup window and automatically populate the ID box on the data entry form.

I'm not sure how to pass the ID value from the lookup pop up window into the text field on the data entry form. At the same time, I'll need the window to close when the ID link is clicked. Can someone please provide guidance and example code?

Thank you,

J
Comment
Watch Question

Commented:
So we're going to need two HTML pages page one can be called index.htm and the popup page popup.htm.

In index.htm paste this code:
<html>
<head>
<script>
function openPopup(targetField){
var w = window.open('popup.htm','color_popup','width=610,height=550,scrollbars=1');
w.targetField = targetField; //create target field variable in popup window with the passed targetField as value
w.focus();
return false;
}

//callback function
function setTargetField(targetField, returnvalue){
if (targetField){
targetField.value = returnvalue;
}
window.focus();
}</script>
</head>
<body>
<form name="theForm">
<input type="text" name="name1" /><a href="#" onclick="return openPopup(document.theForm.name1)">change</a><br />
<input type="text" name="name2" /><a href="#" onclick="return openPopup(document.theForm.name2)">change</a>
</form>
</body>
</html>

In popup.htm paste the following:
<html>
<head>
<script>
function returnValue(value){
if (opener && !opener.closed && opener.setTargetField){
opener.setTargetField(targetField, value);
}
window.close();
}
</script>
</head>
<body>
<a href="#" onclick="returnValue('moose')">CLick</a>
</body>
</html>

In the popup window you will notice the onclick="returnValue('moose')", to change the value returned use this.

Author

Commented:
Wow! I didn't realize it was going to be that complicated (compared to what I had been looking at). I've been trying to get something like this to work.

<a href="javascript:window.opener.document.getElementById("DonorID").value = 1001;">1001</a>

Is something like this not the best approach? From the data entry form, the lookup is either going to be a DonorID or a SolicitorID. I can pass the correct field value (form element name) to the javascript via ASP.

The issue I'm having with the item above is that there's currently a syntax error when I execute it. I'm trying to figure that out right now.

Commented:
I just copied and pasted from here back to testing and no problem.

It might be this function though at fault:

function openPopup(targetField){
var w = window.open('popup.htm','popup','width=610,height=550,scrollbars=1');
w.targetField = targetField;
w.focus();
return false;
}

If you copy and paste that over the top of the other one, that may fix it.

Once you have the javascript in the head of the page the way to call it is very simple.

I have not used the method your suggesting but I know this works.
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
This is just a combination of both of yours, but I think it'll work...

<script>
function changeFormValue(val) {
    window.opener.document.getElementById("DonorID").value = val;
    window.close();
}
</script>

<a href="javascript://" onclick="changeFormValue(1001)">1001</a>


If this doesn't work, what's the syntax error that you're getting, and on what line?

Author

Commented:
OK, I'm almost there. Here's the code I'm currently using.

<a href="javascript:window.opener.formAddItems.DonorID.value=1004;window.close;">1004</a>

When I click the link, the pop up window stays open and shows [Object]. The value is passed correctly from the popup to the data entry form. I just need to figure out how to get the popup closed and we have a winning solution. Any suggestions?
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks to both of you for your input.

ncoo - that solution was a little more complicated than I was hoping for, but your input definitely helped me get to the desired solution. Thanks again.

J
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.