如何在文件上传后提交表单,同时在帖子[]中包含一些数据

时间:2017-10-18 05:45:13

标签: php html forms file-upload phpexcel

嗨我有以下html表单,我用它来上传.xlx文件。

<form action="dashboard.php" method="post" enctype="multipart/form-data">
                            <div class="file-upload">  


                                <div class="col-md-12 m-b-15">
                                    <div class="col-md-3">
                                        Note No :
                                    </div>
                                    <div class="col-md-8">
                                        <input class=" form-control m-b-15 " id="note_number_for_the_alert" name="note_number_for_the_alert" readonly="">                              
                                    </div>
                                </div> 
                                <div class="row">
                                    <div class="col-md-8 m-l-10">

                                        <div class=" formError validateAlert" id="alert_to_upload_file_in_vendor_delivery_note_management" >
                                            <div class="formErrorContent" id="test">please Select the .XLSX file <br></div>
                                            <div class="formErrorArrow"></div>                                
                                        </div>
                                        <input type="file" id="file" name="file2" multiple="multiple" />
                                        <p style="text-align: right; margin-top: 20px;">
                                            <input type="submit"  value="Upload Files" name="submit2" class= "btn btn-success" />
                                        </p>
                                    </div>
                                    <div class="col-md-4"></div>
                                </div>


                            </div>
                        </form>

以下用于上传文件的phpexcel代码行。

$uploadedStatus = 0;
$name2 = '';
if (isset($_POST["submit2"])) {
    if (isset($_FILES["file2"])) {

        if ($_FILES["file2"]["error"] > 0) {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        } else {
            if (file_exists($_FILES["file2"]["name"])) {
                unlink($_FILES["file2"]["name"]);
                $uploadedStatus = 2;
            }
            $name = basename($_FILES['file2']['name']);
            $name2 = explode('.', $name);
            if ($name2[count($name2) - 1] == 'csv' || $name2[count($name2) - 1] == 'xlsx') {

                $target_path = "uploads/programming/";
                $target_location = $target_path . basename($_FILES['file2']['name']);

                move_uploaded_file($_FILES["file2"]["tmp_name"], $target_location);
                $uploadedStatus = 1;
}


        }
    } else {
        echo "No file selected <br />";
    }
    }
}

这两个完全分开工作,文件上传没有问题。 但我需要使用相同的时间向dashboard.php发送一些值。

当前文件上传页面重定向到dashboard.php,任何提交表单的建议一旦获得$ uploadedStatus = 1,文件上传过程完成后。

1 个答案:

答案 0 :(得分:1)

要回答您的问题,您可以使用JQuery上传文件。因此页面无法重新加载。当您按下上传按钮时,显示加载图像会告诉用户您正在处理上传。

<form action="dashboard.php" method="post" enctype="multipart/form-data" id="form_id">

JQuery&amp; Ajax

function submitForm() {
  var formData = new FormData($("#form_id"));
  // show your loader image

    $.ajax({
        url: upload.php,
        type: 'POST',
        data: formData,
        async: false,
        success: function (data) {
            alert(data)
           // hide your loader image
        },
        cache: false,
        contentType: false,
        processData: false
    });
}

注意:Formdata不适用于旧浏览器。有一些后备,请阅读此thread