4

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.

0

1 Answer 1

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);
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.