Using Javascript to dynamically generate file upload boxes
<p>I'm trying to write an html/javascript set (to be interpreted in PHP) that allows the user to select a file from a , and then as soon as that file is selected it displays another . </p>

<pre><code>function displayFileInput() {
var counter = document.getElementById("counter").value;
var n = (parseInt(counter)+1).toString();
var element = document.getElementById("container");
var string = "&lt;br /&gt;&lt;input type = \"file\" name = \"file"+n+"\" onchange =\"javascript:displayFileInput()\"  /&gt;Default:&lt;input type = \"radio\" name = \"main_picture\" value = \""+n+"\"  /&gt;";
element.innerHTML = element.innerHTML + string;
document.getElementById("counter").value=n;
return false;}
</code></pre>

<p>The HTML code I'm using is this:</p>

<pre><code>&lt;form enctype=\"multipart/form-data\" method = \"post\" action=\"newPost.php\" id = \"rental_form\"&gt;
&lt;input type = \"text\" name = \"rent_name\" size = \"30\" value = \"Insert your post title here\"/&gt;&lt;br /&gt;
&lt;textarea rows= \"20\" cols = \"100\" name = \"rent_posttext\"&gt;
Insert your post text here!
&lt;/textarea&gt;&lt;br /&gt;
&lt;div id = \"container\"&gt;
&lt;input type = \"file\" name = \"file1\" onchange = \"javascript:displayFileInput()\" /&gt;
Default:&lt;input type = \"radio\" name = \"main_picture\" value = \"1\"  /&gt;
&lt;/div&gt;
&lt;input type = \"submit\" /&gt;
&lt;input type = \"text\" id = \"counter\" value = \"1\" style = \"display:noe;\"&gt;
</code></pre>

<p>Everything seems to work at first, but the problem is this: as soon as I select a file, it correctly displays another file box, but gets rid of the information in the previous file. For instance, in Google Chrome, next to the "choose file" input button it will say "No file chosen."</p>

<p>When I submit the form to my PHP script, it recognizes the files, but registers a "4" error for each one which means that no file was uploaded. Somehow, it's getting rid of all the file information when it dynamically generates a new file input. Help!</p>
