AJAX发送的文件和变量到PHP [英] ajax send file and variable to php

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

问题描述

我使用Ajax调用要上传的文件,通过PHP处理。该文件应被放置在根据一个jquery变量的特定目录。我可以上传文件,但随着文件不能传递变量。 PHP报告一个未定义的索引错误。

I'm using an ajax call to upload a file, handled by PHP. The file should be placed in a specific directory based on a jquery variable. I can get the file to upload, but cannot pass the variable along with the file. PHP reports an undefined index error.

阿贾克斯code:

var fd = new FormData();    
fd.append( 'file', document.getElementById('select').files[0]);
$.ajax({
    url: 'test.php',
    type: 'POST',
    data: fd,
    processData: false,
    contentType: false,
    success: function(e){
        // some code here
    }
});     

我试图改变的数据属性设置为FD +'和; myVar的='+ myVar的,但是PHP不能正确地分析数据,并同时为$ _FILES ['文件']变量返回undefined指数的错误,以及在$ _ POST ['myVar的']变量。

I tried changing the data property to "fd+'&myVar='+myVar, however PHP cannot parse the data correctly and returns undefined index error for both the $_FILES['file'] variable as well as the $_POST['myVar'] variable.

我如何能同时发送的​​文件和一个变量?

How can I send both the file and a variable?

推荐答案

如果你需要另一种形式的字段,调用fd.append第二次:

If you need another form field, call fd.append a second time:

fd.append('file', document.getElementById('select').files[0]);
fd.append('myVar',myVar);

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

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