$ _FILES没有值[重复]
This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/4545081/how-to-do-file-upload-using-jquery-serialization" dir="ltr">how to do file upload using jquery serialization</a>
                            <span class="question-originals-answer-count">
                                (8 answers)
                            </span>
                    </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/2320069/jquery-ajax-file-upload" dir="ltr">jQuery Ajax File Upload</a>
                            <span class="question-originals-answer-count">
                                (24 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2020-01-23 07:51:26Z" class="relativetime">3 months ago</span>.</div>
        </div>
    </aside>

First of all, I got this enctype="multipart/form-data" in my form tag.

When I send a data as a text, it proceeds and give me data.

<input type="text" class="form-control" name="attachment">

But when I change the type as "file", it don't send a data.

<input type="file" class="form-control" name="attachment">

Here is my HTML File:

<form id="form_attachment" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="employee_number" value="<?= $_GET[md5('employee_number')] ?>">

     <label class="bmd-label-floating">Choose Attachment</label>
     <input type="file" class="form-control" name="attachment">

     <label class="bmd-label-floating">Remarks</label>
     <textarea name="remarks" rows="5" class="form-control"></textarea>

     <input type="submit" id="btn_attachment" value="Insert" class="btn btn-info" style="float:right" /><br><br>
 </form>

AJAX:

$('#form_attachment').on("submit", function(e) {
    e.preventDefault();
    $.ajax({
        type: "POST",
        url: "../ajax/insert_attachment.php",
        data: $("#form_attachment").serialize(),
        beforeSend: function() {
        $('#btn_attachment').val("Updating");
    },
    success: function(response) {
        $('#form_attachment')[0].reset();
        $('#btn_attachment').val("Update");
        $('#modal_attachment').modal('hide');
        // $('#div_attachment').html(response);
        md.onboarding('bottom', 'right', response);
    }
    });
    e.preventDefault();
});

PHP:

$employee_number = $_POST['employee_number'];
$attachment = $_FILES["attachment"]["name"];
$attachment_tmp = $_FILES['attachment']['tmp_name'];
$remarks = $_POST['remarks'];

It gives me this error:

NOTICE: Undefined index: attachment in 
C:\XAMPP\HTDOCS\AJAX\INSERT_ATTACHMENT.PHP on line 6
NOTICE: Undefined index: attachment in 
C:\XAMPP\HTDOCS\AJAX\INSERT_ATTACHMENT.PHP on line 7
</div>
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问