Link to home
Start Free TrialLog in
Avatar of brownmetals
brownmetals

asked on

Popup Window to Pass Value to Parent Window On Close

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
Avatar of ncoo
ncoo

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.
Avatar of brownmetals

ASKER

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.
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.
SOLUTION
Avatar of ncoo
ncoo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?

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?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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