SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict rabbitdog's Avatar
    Join Date
    Jul 2001
    So. Tenn.
    0 Post(s)
    0 Thread(s)

    Populating form boxes from a popup window link

    Here's what I'm trying to do:

    I have a popup window which is launched from a parent window. From that popup window, I'd like to populate x number of fields in the parent window by clicking a link.

    I've done the google thing and pulled the following code: window.opener.myForm.myField.value = myVar which seems like it would do the trick, HOWEVER, I'm looking for a way to pass multiple field/value pairs back to the parent window.

    I guess I could do it with a function that would be called for each field/value pair...but that seems like an in-elegant way to go about it. I'm no javascript expert so any advice would be welcome.

    Mr Vector
    High quality, royalty free, vector graphics
    for t-shirt artists and graphic/web designers.

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Dallas, TX
    0 Post(s)
    0 Thread(s)
    Quote Originally Posted by rabbitdog
    I guess I could do it with a function that would be called for each field/value pair...but that seems like an in-elegant way to go about it.
    Just do them all from one function

    function updateParentForm()
    	var f = opener.forms['formName'];
    	f.element1.value = var1;
    	f.element2.value = var2;
    	f.element3.value = var3;
    You get the idea
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    0 Post(s)
    0 Thread(s)
    A little more elaborate...fwiw.
    <title>Form Page</title>
    <style type="text/css">
    form {font-weight:bold;}
    a:link {font:bold 14px verdana;color:white;}
    a:visited {font:bold 14px verdana;color:skyblue;}
    a:hover {font:bold 14px verdana;letter-spacing:2px;}
    a:active {font:bold 14px verdana;color:lightblue;}
    <script type="text/javascript" language="javascript">
    var Fwin = null;
    var w = 140;
    var h = 180;
    var l = (screen.availWidth - w) / 2;
    var t =  (screen.availHeight - h) / 2;
    function openFormWin(url)
    	Fwin = open(url,'Fwin','width='+w+',height='+h+',left='+l+',top='+t+',status=0');
    	if (Fwin && !Fwin.closed)
    	return false;
    <body style="background:steelblue;" onload="document.mainform.reset()">
    <a href="#null" onclick="return openFormWin('formpop.html')">Show Choices</a>
    <form name="mainform"> 
    <input name="user_name" type="text">___name<br>
    <input name="user_address" type="text">___address<br>
    <input name="user_city" type="text">___city<br>
    <input name="user_state" type="text">___state<br><br>
    <select name="country">
    <option selected="selected">-- country --</option>
    <option value="USA">USA</option>
    <option value="Scotland">Scotland</option>
    <option value="Romania">Romania</option>
    <select name="potatoes" multiple="multiple" size="3">
    <option value="fried">fried</option>
    <option value="baked">baked</option>
    <option value="burned">burned</option>
    &nbsp;&nbsp;&nbsp;<input name="temp" type="radio" value="hot"> hot<br>
    &nbsp;&nbsp;&nbsp;<input name="temp" type="radio" value="cold"> cold<br><br>
    ___yes? <input name="yes" type="checkbox" value="yes"><br>
    ___no! <input name="no" type="checkbox" value="yes">
    <style type="text/css">
    a:link {font:bold 14px verdana;color:navy;}
    a:visited {font:bold 14px verdana;color:blue;}
    a:hover {font:bold 14px verdana;letter-spacing:2px;}
    a:active {font:bold 14px verdana;color:white;}
    <script type="text/javascript">
    var sOpenerFormName = 'mainform';
    var autoclose = false; //close window
    var transferVals = new Array();
        transferVals[0] = {
    			user_name: 'Al Sharpton' ,
    			user_address: '33 Windy Lane' ,
    			user_city: 'Paramus' ,
    			user_state: 'NJ' ,
    			country: 'USA' ,
    			potatoes: [ 'fried' , 'baked' , 'burned' ] ,
    			temp: 'hot' ,
    			yes: 'yes'
        transferVals[1] = {
    			user_name: 'Joe Z. Blow' ,
    			user_address: '16 Park Crescent' ,
    			user_city: 'Shmeck' ,
    			country: 'Romania' ,
    			potatoes: 'burned' ,
    			temp: 'cold' ,
    			no: 'no'
        transferVals[2] = {
    			user_name: 'Dracula' ,
    			user_address: '11 Jugular Court' ,
    			user_city: 'Klarn' ,
    			country: 'Scotland' ,
    			potatoes: 'baked' ,
    			temp: 'hot'
    function to_opener(oVals)
    	if (!opener || opener.closed || typeof opener.document.forms[sOpenerFormName] == 'undefined')
    		return false;
    	var oOpenerForm = opener.document.forms[sOpenerFormName];
    	oOpenerForm.reset(); //clear form
    	var fieldname, opt, i, j, optVal, selArr, opener_els = oOpenerForm.elements;
    	for (fieldname in oVals)
    		if (obj = opener_els[fieldname])
    			if (typeof obj[0] == 'undefined' || /select/.test(obj.type))
    				el = obj;
    			else el = obj[0];
    			switch (el.type)
    				case 'text' :
    				case 'textarea' :
    				case 'hidden' :
    					el.value = oVals[fieldname];
    				case 'checkbox' :
    				case 'radio' :
    					grp = opener_els[]; i = 0;
    					if (typeof grp[0] == 'undefined') 
    						opener_els[fieldname].checked = true;
    					else while (el = grp[i++]) 
    						if (el.value == oVals[fieldname])
    							el.checked = true;
    				case 'select-one' :
    					i = 0;
    					while (opt = el.options[i++])
    						if (opt.value == oVals[fieldname])
    							opt.selected = true;
    				case 'select-multiple' :
    					if (typeof oVals[fieldname] == 'string')
    						selArr = [oVals[fieldname]];
    					else selArr = oVals[fieldname];
    					i = 0;
    					while (optVal = selArr[i++])
    						j = 0;
    						while (opt = el.options[j++])
    							if (opt.value == optVal)
    								opt.selected = true;
    	if (autoclose)
    	return false;        
    <body style="text-align:center;margin-top:30px;background:#82a4c8;">
    <a href="#null" onclick="return to_opener(transferVals[0])">choice 1</a><br /><br />
    <a href="#null" onclick="return to_opener(transferVals[1])">choice 2</a><br /><br />
    <a href="#null" onclick="return to_opener(transferVals[2])">choice 3</a><br /><br />
    ::: certified wild guess :::


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts