Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML: Copy form fields to another form including FILE input field?

I came to see that form file input field value cannot be set with javascript for security reasons.

I just want to copy a FILE input to another form and post it, I searched for a work around and could not find anything, is it possible?

UPDATE: my code:

function prepareUpload( filevalue ){

document.getElementById('logo').value =filevalue;
var mform = document.getElementById('sleeker');
    ajaxUpload( mform,'<?php echo base_url(); ?>'); // a methods to upload...
}

<input class="input-file-upload" type="file" size="20" name="logodummy" id="logodummy" onchange="prepareUpload( this.value );" />

<form action="" method="post" name="sleeker" id="sleeker" enctype="multipart/form-data" onbeforesubmit="return false;">
            <p><input type="hidden" name="logo" id="logo" /></p>
        </form>

Anything other thatn file input are working fine, and I could receive with $_POST, but $_FILES doesn't have values. And this code alone working fine too. I think this coe is enough?

like image 434
pMan Avatar asked Feb 08 '10 07:02

pMan


People also ask

How do I copy a field in another form?

Simply make a selection of fields and press the 'Copy to form' button at the top of the page. You'll be prompted to selected the destination form you'd like to copy the selected fields to. Make your selection and you're done! This opens in a new window.

How do I copy values from one input field to another?

Simply register an input even handler with the source textfield and copy the value to the target textfield .

How do we pre populate a form input field with some text?

By giving the input tag a value attribute with whatever text you want pre-populated.

Do input fields need to be in a form?

Yes, you can have a valid input without a form.


1 Answers

Yes, you can place the <input type="file"> outside your HTML form, and then use the onChange event to fill an <input type="hidden"> within the form that gets posted:

<input type="file" 
       onchange="document.getElementById('hidden_file').value = this.value;" />

<form method="POST">
    <input type="hidden" id="hidden_file" value="" />
    <input type="submit" />
</form>

However in modern browsers, you will only be able to access the file name, and not the full path. You may want to check the following Stack Overflow posts for further information on this topic:

  • Can’t get the complete address while uploading a file
  • How to get the file path from HTML input form in Firefox 3

UPDATE:

The original question made me think that you only needed to copy the "file name" to another HTML form, and not the whole <input type="file"> representation.

Further to the update, I assume you meant something like this:

<input type="file" 
       onchange="document.getElementById('hidden_file').value = this.value;" />

<form method="POST">
    <input type="file" id="hidden_file" value="" />
    <input type="submit" />
</form>

Unfortunately the above does not work. Firefox will return "Security error code: 1000" if you try the above example.

As for some workarounds, you may want to the check David Dorward's suggestions:

  • Using cloneNode
  • Moving the input field with appendChild before submitting the form
like image 78
Daniel Vassallo Avatar answered Oct 01 '22 00:10

Daniel Vassallo