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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

populate text field with popup data 1

Status
Not open for further replies.

derwent

Programmer
May 5, 2004
428
GB
There is a form for our cms that uploads a story to the DB. They want an image upload, so I am opening a popup window to upload the image.

I want to then pass the filename of the upload back from the popup window to populate the form field in my original page.

Is this possible?

The code on the original is like this

Code:
<input type="text" name="product_picture" value=""> <a href="upload.asp">upload image</a>

the popup minus the asp upload scripts:

Code:
<form name="frmSend" method="POST" enctype="multipart/form-data" action="upload.asp">
    File 1: <input name=attach1 type=file size=35><br>
    File 2: <input name=attach2 type=file size=35><br>
    File 3: <input name=attach3 type=file size=35><br>
    File 4: <input name=attach4 type=file size=35><br>
    <br> 
    <inputtype=submit value="Upload">
    </form>
<br>
Close window

when they have uploaded the image then click close window, the filename is passed to the input on the first page

thanks
 
Change your popup script to something like this:

Code:
<form name="frmSend">
    File 1: <input name=attach1 type=file size=35><br>
    File 2: <input name=attach2 type=file size=35><br>
    File 3: <input name=attach3 type=file size=35><br>
    File 4: <input name=attach4 type=file size=35><br>
    <br>
    <input type="button" value="Upload" onclick="opener.document.formname.fieldname.value = this.form.attach1.value; return false;">
    </form>

This will make the field named "fieldname" in the form "formname" in the parent window get populated with the value from "attach1".

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
Many thanks clflava, it puts the data into the correct field.

Is it possible also to when the user submits the form, it submits the form AND populates the field? I have the code to update the form field on submit

<input type=submit value="Upload" onclick="opener.document.add_form.product_picture.value = this.form.attach1.value; return false;">
 
When the file is uploaded the page then reloads to display the filename of the file uploaded. I tried displaying this in a form field and then a button to put this into the original field but this didn`t work.

The problem when I pass the data directly back to the original form is that all of the file location is passed i.e. c:\my documents\images\myfile.jpg whereas all I want to be passed back to the original is myfile.jpg

is al of this possible?
 

Use the JavaScript string method of "lastIndexOf" to help you out here:

Code:
var wholePath = 'c:\\my documents\\images\\myfile.jpg';
var filenameOnly = wholePath.substr(wholePath.lastIndexOf('\\') + 1);
alert(filenameOnly);

Hope this helps,
Dan
 
sorry I`m a real dipstick when it comes to javascript, where do I put this code?
 

I'm a real dipstick when it comes to psychic code reading, so you'll have to post some of your code before that can be answered exactly.

However - it will most likely be put in your form submission routines (onsubmit event handler).

Hope this helps,
Dan
 
I am now using this that submits my form on the popup and completes the field in the original page

<input type=submit value="Upload" onclick="opener.document.add_form.product_picture.value = this.form.attach1.value; return true;">

unfortunately as it relays back all of the info from the input=file field, it is returning e.g. c:\my documents\my pictures\myimage.gif

is it possible for it to only send back the filename e.g. myimage.gif??

thanks again
 

Hi derwent,

If you merge the code you've now got with the middle line of my previous post, it should work as expected. Something like this:

Code:
<input type="submit" value="Upload" onclick="(); return(true);" />

and then add the fillForm function to the HEAD section of the file:

Code:
<script type="text/javascript">
<!--
	function fillForm() {
		var filename = this.forms['form'].elements['attach1'].value;
		opener.document.forms['add_form'].elements['product_picture'].value = filename.substr(filename.lastIndexOf('\\') + 1);
	}
//-->
</script>

Hope this helps,
Dan
 

Hmm - I seem to have cut the function name instead of copying it. The input line should read:

Code:
<input type="submit" value="Upload" onclick="fillForm(); return(true);" />

Sorry for the confusion!

Dan
 
Many thanks for this Dan, however nothing is being passed back?!?!

I have put the name of the image upload form into the this.forms area

Code:
<script type="text/javascript">
<!--
    function fillForm() {
        var filename = this.forms['frmSend'].elements['attach1'].value;
        opener.document.forms['add_form'].elements['product_picture'].value = filename.substr(filename.lastIndexOf('\\') + 1);
    }
//-->
</script>

and placed it in the head.
 

Try putting a few alert statements in to see if the function is being called, and also what the value of filename is:

Code:
function fillForm() {
        alert('Getting here!');
        var filename = this.forms['frmSend'].elements['attach1'].value;
        alert('Value of filename is: ' + filename);
        opener.document.forms['add_form'].elements['product_picture'].value = filename.substr(filename.lastIndexOf('\\') + 1);
    }

Dan
 
The Getting here alert box popped up, but not the filename one
 

If the second alert is not popping up, then the preceeding line must be causing a JavaScript error.

- Is your form definately named "frmSend"?
- Is your filename box definately within that form?
- Is your filename box definately names "attach1"?

Try running it in Firefox and pop open the JavaScript console - it may well help you with a meaningful error message.

Hope this helps,
Dan
 
Didn`t know about the javascript console :eek:)

I ran it and it had this to say

Error: this.forms has no properties
 
haha, it works!!!!!

Many thanks BillyRayPreachersSon. I`ve learnt a lot from this thread and will now spend a few days on w3school javascript area when this project is out of the way :eek:D

thanks again matey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top