Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP AJAX jQuery send file and text

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.

like image 505
diank Avatar asked Mar 11 '23 15:03

diank


1 Answers

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);
        }
    });
});
like image 199
Haresh Vidja Avatar answered Mar 19 '23 09:03

Haresh Vidja