Forum Moderators: open

Message Too Old, No Replies

Popup Windows & Form Fields

         

mightymouse3062

3:07 am on Jan 6, 2009 (gmt 0)

10+ Year Member



Good Evening,
I am trying to create a form for a website and I want to be able to create a page that will insert the information generated into a forms field. How do I do that?
Here is what I have done so far:
I have a page (called index.php) where it has a form called Admin and in the form I have field called location. Next to location is a button called select and onclick it does this:
function openLinkCreation(){
linkcreator=window.open("location.php","","width=300,height=210")
}

the file location.php creates the info I want and has a button to click to insert it into the location field.

That is as far as I have gotten in this. I want to be able to insert the info into the field (without reloading the "parent" window) and close the window that was created by the openLinkCreation function.

Thank you for your assistance,
Mike

daveVk

4:31 am on Jan 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



location.php can see the parent page via window.opener so to access element in parent, to return results use code like

var w = window.opener;
var wd = w.document;
wd.getElementById( 'answer' ).value = ... // or to where ever its needed

mightymouse3062

4:08 pm on Jan 6, 2009 (gmt 0)

10+ Year Member



I am kinda week is Javascript so I might be doing this completely wrong. I used the code you said:
<script type="text/javascript">
var w = window.opener;
var wd = w.document;
wd.getElementById( 'location' ).value = <?php echo $output; ?>
</script>

Here is the form that I want to insert it into:
<form name="Admin" method="post" action="<?php echo $root_1 . "/administrator/index.php?act=menu&task=save&id=" . $mid; ?>">
<input type="location" name="location" onClick="openLinkCreation()" value="<?php echo $menudata['location']; ?>">
<input type="submit" name="Submit" value="Save">
</fieldset>
</form>

All of the php tags are used to pull information from the database so there is already data in the field. For some reason this code is not working and I am clueless on where that is. What am I doing wrong?

Thank you very much,
Mike

rocknbil

5:23 pm on Jan 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard mightymouse3062.

Two observations:

1. what is 'type="location"'? Even if that were a valid form field type, it would conflict with the reserved Javascript word location which is the document's . . . location. So do not use location for your form field names or id's.

2. Get in the habit of adding id's to your form elements. They can be the same as the name attribute with the exception of radio buttons.

<input type="text" name="loc" id="loc">

This allows you to refer to specific elements using document.getElementById() rather than document forms[form_name].fieldname, and is a lot easier to follow. All you have to remember is an id is unique, do not have two of the same ID's in a given document.

The basic premise is parent opens child window, parent is referred to as window.opener, and child can send data back to opener. one of many threads [webmasterworld.com] dealing with window.opener, with working examples.

once you open the new window, in that new window you need only to do something like this:

<input type="button" onClick = window.opener.document.getElementById('loc').value=document.getElementById('loc').value; return false; window.close();" value="Close Me">

... Where the new window also has a field "loc". "document.getElementByID . . ." refers to the current document (the pop up.) "window.opener.document.getElementById" refers to the parent window.

Of course, it would be best to move that code into a function the head of the document so the button code is not bloated, but you get the idea.

daveVk

9:41 pm on Jan 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



With exceptions rocknbil noted looks good, should also check if parent is still open.

<script type="text/javascript">
var w = window.opener;
if ( w ) {
var wd = w.document;
wd.getElementById( 'loc' ).value = <?php echo $output; ?>
}else{alert(...) }
</script>

Here is the form that I want to insert it into:
<form name="Admin" method="post" action="<?php echo $root_1 . "/administrator/index.php?act=menu&task=save&id=" . $mid; ?>">
<input id="loc" name="loc" onClick="openLinkCreation()" value="<?php echo $menudata['location']; ?>">
<input type="submit" name="Submit" value="Save">
</fieldset>
</form>