通过Ajax发送FormData + js变量

时间:2016-02-20 06:09:44

标签: javascript jquery ajax forms variables

我有这个Ajax发送多个图像:

$('#btn').on("click", function () {
        var formData = new FormData($("#form1")[0]);
        var path = "php/upload/adm_prodpictures.php";
        $.ajax({
            url: path,
            type: "POST",
            data: formData,
            contentType: false,
            processData: false,
            success: function (stuff) {
                $("#resp").html(stuff);
            }
        });
    });
});

我必须在php端处理这些图像并将它们插入到mysql数据库中。所以要以正确的方式插入,我必须发送一个javascript变量。如何将此变量附加到发送的“包”?

2 个答案:

答案 0 :(得分:3)

要附加参数,只需使用append()方法:

formData.append("param", "value");

答案 1 :(得分:0)

解决。我补充说:

formData.append( 'IPID',ID);

所以最后我的ajax是:

$('#btn').on("click", function () {
        var formData = new FormData($("#form1")[0]);
        formData.append('ipid',id); //id is the variable that has the data that I need
        var path = "php/upload/adm_prodpictures.php";
        $.ajax({
            url: path,
            type: "POST",
            data: formData,
            contentType: false,
            processData: false,
            success: function (stuff) {
                $("#resp").html(stuff);
            }
        });
    });
});

在php端我抓住了它:

$pid = ($_POST['ipid']);