Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing a variable for popup to parent?

Status
Not open for further replies.

peter11

Instructor
Mar 16, 2001
334
US
I have a page with a link that opens a popup. The popup allows the user to enter a number into a form. When I hit the submit button I want the value entered to be passed back to the parent and the parent page to refresh.

Any suggestions?

Thanks
 
pass variable to parent:

Code:
<input type="button" onclick="opener.document.forms['formName'].elements['elementName'].value = this.form.elements['myOtherField'].value;" />

refresh parent:

Code:
opener.location = opener.location;

but i don't see the use in passing a value, then refreshing - as this will eliminate the fact that you passed a variable.

*cLFlaVA
----------------------------
[tt]your mom goes to college[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
That did not work but it may be my explanation.
The form is on the popup, the user will enter a number in the text box(name = number).

Upon submit the popup will close and the value in the textbox will be passed to a variable(numartifact)on the parent page and the parent page will refresh to show the change.

Thanks
 
No sweat:

Using the window.open() method to generate the popup window (I presume) you can, as cLFlaVA mentioned - communicate with the opening window via the opener property.

Then it's just a matter of filling in say a hidden field on your main form and calling it's submit() method to trigger the refresh - keeping the integrity of the data you've entered.

These little snippets should set you on the right track:

mainpage.html
Code:
<html>
<head>
<script language="JavaScript" type="text/javascript">
function getValue(){
 var win = window.open('subpage.html');
}

//simulate a bit of server process
function simulateServerProcessing(){
 var arrSearch = location.search.split("?");
 if(arrSearch.length > 1){ // has been submitted
  document.getElementById("textoutput").style.display = "block";
  document.getElementById("textinput").style.display = "none";
  arrValuePairs = arrSearch[1].split("&");
  var strTyped = "";
  var strSelected = "";
  for(var i = 0; i < arrValuePairs.length; i++){
   arrCurrent = arrValuePairs[i].split("=");
   switch(arrCurrent[0]){
    case "textinput1" : {
	 strTyped = arrCurrent[1];
	 break;
	}
    case "hiddenvalue" : {
	 strSelected = arrCurrent[1];
	 break;
	}
	default : {
	 break;
	}
   }
  }
  document.getElementById("textoutput").innerText = "You Typed " +
                                                    strTyped +
													" and selected " +
													strSelected + ".";
 }
 else{ // is loaded for first time
  document.getElementById("textoutput").style.display = "none";
  document.getElementById("textinput").style.display = "block";
 }
}
</script>
</head>

<body onLoad="simulateServerProcessing();">
<form action="mainpage.html" method="get" id="mainform">
<div id="textinput">
Type Some Text: <input type="text" name="textinput1" value=""/><br/>
<br/>
<input type="button" name="button1" value="Get Value" onClick="getValue()"/>
<input type="hidden" id="hiddenvalue" name="hiddenvalue" value="" />
</div>
<div id="textoutput">
</div>
</form>
</body>
</html>

and subpage.html
Code:
<html>
<head>
<head>
<script language="JavaScript" type="text/javascript">
function setValue(){
 var mummy = window.opener;
 var objSelect = document.getElementById("valueselect");
 var strValue = objSelect.options[objSelect.selectedIndex].text;
 mummy.document.getElementById("hiddenvalue").value = strValue;
 mummy.document.getElementById("mainform").submit();
 self.close();
}


</script>
</head>

<body>
Select a Value: 
<select id="valueselect">
<option>one</option>
<option>two</option>
<option>three</option>
</select><br>
<input type="button" onClick="setValue()" value="OK" />
</body>
</html>

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top