SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict rabbitdog's Avatar
    Join Date
    Jul 2001
    Location
    So. Tenn.
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    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.

    Thanks!
    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
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    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

    Code:
    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
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A little more elaborate...fwiw.
    Code:
    <html>
    <head>
    <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;}
    </style>
    <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)
    		Fwin.focus();
    	return false;
    }
    
    </script>
    </head>
    <body style="background:steelblue;" onload="document.mainform.reset()">
    <br>
    <a href="#null" onclick="return openFormWin('formpop.html')">Show Choices</a>
    <br><br><br>
    <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>___country<br><br>
    <select name="potatoes" multiple="multiple" size="3">
    <option value="fried">fried</option>
    <option value="baked">baked</option>
    <option value="burned">burned</option>
    </select>___potatoes<br><br>
    ___temperature<br>
    &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">
    </form>
    </body>
    </html>
    [formpop.html]
    Code:
    <html>
    <head>
    <title>CHOOSER</title>
    <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;}
    </style>
    <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];
    					break;
    				case 'checkbox' :
    				case 'radio' :
    					grp = opener_els[el.name]; 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;
    					break;
    				case 'select-one' :
    					i = 0;
    					while (opt = el.options[i++])
    						if (opt.value == oVals[fieldname])
    							opt.selected = true;
    					break;
    				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;
    					}
    					break;
    			}
    		}
    	}
    	if (autoclose)
    		self.close();
    	return false;        
    }
    
    </script>
    </head>
    <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 />
    </body>
    </html>
    ::: certified wild guess :::


Bookmarks

Posting Permissions

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