PHP AJAX jQuery发送文件和文本 [英] PHP AJAX jQuery send file and text

查看:61
本文介绍了PHP AJAX jQuery发送文件和文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有一个简单的表格,可以输入文字和文件.我想在输入中输入一些名称并添加文件.这是我的HTML代码:

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>

这是我简单的jQuery代码:

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);
        }
    });
});

在PHP中,我使用文件等.但是我想要文件和名称(输入).如何和输入(名称)中的值? 谢谢.

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.

推荐答案

我希望它会对您有所帮助.

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);
        }
    });
});

这篇关于PHP AJAX jQuery发送文件和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆