简体   繁体   中英

Send FormData + js variable by Ajax

I have this Ajax to send multiple images:

$('#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);
            }
        });
    });
});

I have to process this images in the php-side and insert them in a mysql db. So to insert in the proper way, I have to send a javascript variable. How can I append this variable to the 'bundle' that is sent?

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

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

Solved. I add:

formData.append('ipid',id);

So finally my ajax is:

$('#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);
            }
        });
    });
});

And in the php-side I catch it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM