最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - PHP AJAX jQuery send file and text - Stack Overflow

programmeradmin1浏览0评论

Hi everyone I have a simple form with input for text and file. I want to type some name in input and add file. Here is my HTML code:

Name: <input type="text" id="name" name="name"><br>
File: <input type="file" id="file" name="file"><br>
<button id="submit">Submit</button>

And this is my simple jQuery code:

$('#submit').click(function() {
    var file_data = $('#file').prop('files')[0];   
    var form_data = new FormData();                  
    form_data.append('file', file_data);
    $.ajax({
        url: 'include/upload_idcard.php',
        dataType: 'text',
        cache: false,
        contentType: false,
        processData: false,
        data: form_data,                         
        type: 'post',
        success: function(result){
            alert(result);
        }
    });
});

And in PHP I take the file and etc. But I want the file and the name(input). How to the and the value in input(name)? Thanks.

Hi everyone I have a simple form with input for text and file. I want to type some name in input and add file. Here is my HTML code:

Name: <input type="text" id="name" name="name"><br>
File: <input type="file" id="file" name="file"><br>
<button id="submit">Submit</button>

And this is my simple jQuery code:

$('#submit').click(function() {
    var file_data = $('#file').prop('files')[0];   
    var form_data = new FormData();                  
    form_data.append('file', file_data);
    $.ajax({
        url: 'include/upload_idcard.php',
        dataType: 'text',
        cache: false,
        contentType: false,
        processData: false,
        data: form_data,                         
        type: 'post',
        success: function(result){
            alert(result);
        }
    });
});

And in PHP I take the file and etc. But I want the file and the name(input). How to the and the value in input(name)? Thanks.

Share Improve this question asked Jul 21, 2016 at 12:59 diankdiank 6382 gold badges11 silver badges19 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 8

I hope it will be helpful to you.

$('#submit').click(function() {
    var file_data = $('#file').prop('files')[0];   
    var form_data = new FormData();                  
    form_data.append('file', file_data);
    form_data.append('name', $("#name").val());
    $.ajax({
        url: 'include/upload_idcard.php',
        dataType: 'text',
        cache: false,
        contentType: false,
        processData: false,
        data: form_data,                         
        type: 'post',
        success: function(result){
            alert(result);
        }
    });
});
发布评论

评论列表(0)

  1. 暂无评论