使用表单提交上载具有相同字段名称的多个文档

时间:2016-05-20 12:11:54

标签: javascript html

我想使用html中的表单提交,使用相同的字段名称上传多个文档(PDF,doc,txt等,...)。 我已经使用w3schools.com的PHP 5 File Upload尝试了所有内容。

<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>

但我不知道如何将这些多个文档传递给表单提交功能。我在This jsFiddle上面写了相同的代码,需要使用javascript提交。

由于

2 个答案:

答案 0 :(得分:3)

你可以使用php函数上传多个文件,循环计算总文件和数据,如下所示(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;
    }

答案 1 :(得分:0)

您可以尝试使用file元素的multiple属性,它可以让您选择多个文件。还有你的from缺少了enctype。

    <form action="upload.php" method="POST"  enctype="multipart/form-data">
    <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>

    </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>

    </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>

    </div>
    <div>
        Documents: <input type="file" name="docs" multiple="true"><br>
    </div>
    <div class="clear"></div>
    <input type="submit" value="Submit">
    </form>