admin管理员组

文章数量:1326123

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?

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?

Share Improve this question asked Feb 20, 2016 at 6:09 Pablo De LucaPablo De Luca 8233 gold badges16 silver badges31 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

To append param just use append() method:

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']);

本文标签: javascriptSend FormDatajs variable by AjaxStack Overflow