Kirankumar Kirankumar - 6 months ago 10
Javascript Question

Upload multiple documents with same field name using form submit

I wants to upload mulitple documents(PDF,doc, txt etc,...) with same field name using form submit in html.
I have tried everything with PHP 5 File Upload from w3schools.com.

<form action="upload.php" method="POST">
<div class="left">
<h2>Detail 1</h2>
Name:<input type="text" name="txtName[]"><br>
Card No:<input type="text" name="txtCardNo[]"><br>
Date of Birth:<input type="text" name="txtBdate[]"><br>
Document:<input type="file" name="docs[]"><br>
</div>
<div class="left">
<h2>Detail 2</h2>
Name:<input type="text" name="txtName[]"><br>
Card No:<input type="text" name="txtCardNo[]"><br>
Date of Birth:<input type="text" name="txtBdate[]"><br>
Document:<input type="file" name="docs[]"><br>
</div>
<div class="left">
<h2>Detail 3</h2>
Name:<input type="text" name="txtName[]"><br>
Card No:<input type="text" name="txtCardNo[]"><br>
Date of Birth:<input type="text" name="txtBdate[]"><br>
Document:<input type="file" name="docs[]"><br>
</div>
<div class="clear"></div>
<input type="submit" value="Submit">
</form>


But I dont know how to pass these multiple documents into form submit function. I have wrote above same code in This jsFiddle and need to submit this using javascript.

Thanks

Answer

You can upload multiple files using php function with the loop to count total files and data as shown below (Codeigniter Insert)

    if(!empty($_POST['txtName'])){              
         for($i=0; $i<count($_POST['txtName']); $i++){
             $cont['name'] = $_POST['txtName'][$i];
             $cont['cardno'] = $_POST['txtCardNo'][$i];
             $cont['dob'] = $_POST['txtBdate'][$i];              
             $cont['docs'] = str_replace(' ', '_',$_FILES['docs']['name'][$i]);

            if($cont['docs'] != "")
            {                   
                move_uploaded_file($_FILES["docs"]["tmp_name"][$i], $target_path.$cont['docs']);

            }
            $this->db->insert('detail',$cont);

         }
         return true;
    }else{
        return false;
    }