通过AJAX jQuery表单提交发送表单数据(字符串)和数组

时间:2022-11-24 12:39:36

I've researched several similar answers however I can't find any that show how to send both an array, and a string.

我已经研究了几个类似的答案但是我找不到任何显示如何发送数组和字符串的答案。

I have discovered that .serialize and .serializeArray should allow me to construct the string I need to send the form data and the array to my PHP script, but I cannot understand the syntax to use both of these functions together.

我发现.serialize和.serializeArray应该允许我构造我需要将表单数据和数组发送到我的PHP脚本的字符串,但我无法理解将这两个函数一起使用的语法。

My form is simple:

我的表格很简单:

<form id="email-input">
   <input type="email" name="user-mail" />
</form> 

I wish to submit with AJAX, and send both the value in the form and a javascript array called basketData to my PHP script.

我希望用AJAX提交,并将表单中的值和名为basketData的javascript数组发送到我的PHP脚本。

$.ajax({
            type: "POST",
            url: "scripts/send-mail.php",
            data: {$("#email-input").serialize(),basketData.serializeArray()}
            success: function(data)
            {
                console.log(data);
            }
        });

I'm aware the above is completely syntactically incorrect but I just can't work out how to pass multiple data values through this function.

我知道上面的语法完全不正确,但我无法弄清楚如何通过这个函数传递多个数据值。

2 个解决方案

#1


0  

jQuery:

$.ajax({
    type: "POST",
    url: "scripts/send-mail.php",
    data: {
        email: $('input[name="user-mail"]').val(),
        basketData: basketData
    }
    success: function(data) {
        console.log(data);
    }
});

PHP:

$email = $_POST['email'];
$basketData = $_POST['basketData'];

#2


0  

serializeArray is for converting a jQuery object containing a form into structured data as a precursor for converting it in to a serialized string. It is rarely useful to call it directly, and it can't be called on an array (otherData:basketData.serializeArray()} will throw an array because serializeArray is undefined since basketData isn't a jQuery object).

serializeArray用于将包含表单的jQuery对象转换为结构化数据,作为将其转换为序列化字符串的前体。直接调用它很少有用,并且无法在数组上调用(otherData:basketData.serializeArray()}将抛出一个数组,因为serializeArray未定义,因为basketData不是jQuery对象)。

serialize is for converting a jQuery object containing a form into a string of form encoded data. It is great for dealing with complex forms. It becomes a little fiddly when you want to modify the data.

serialize用于将包含表单的jQuery对象转换为表单编码数据的字符串。它非常适合处理复杂的表格。当您想要修改数据时,它会变得有点繁琐。

Forget about using either of them for this.

忘记使用它们中的任何一个。

The data argument for .ajax accepts a JavaScript object as its value, and then jQuery will encode it as form data using PHP's conventions for complex data structures (which works out nicely for you since you say your array isn't flat and you are using PHP).

.ajax的数据参数接受一个JavaScript对象作为其值,然后jQuery将使用PHP的复杂数据结构约定将其编码为表单数据(由于您说您的数组不是平的并且您正在使用它,因此很适合您PHP)。

Just put the array and the value of the form control into your data object.

只需将数组和表单控件的值放入数据对象中即可。

data: {
    email: $("input[name='user-mail']").val(),
    array: basketData
},

#1


0  

jQuery:

$.ajax({
    type: "POST",
    url: "scripts/send-mail.php",
    data: {
        email: $('input[name="user-mail"]').val(),
        basketData: basketData
    }
    success: function(data) {
        console.log(data);
    }
});

PHP:

$email = $_POST['email'];
$basketData = $_POST['basketData'];

#2


0  

serializeArray is for converting a jQuery object containing a form into structured data as a precursor for converting it in to a serialized string. It is rarely useful to call it directly, and it can't be called on an array (otherData:basketData.serializeArray()} will throw an array because serializeArray is undefined since basketData isn't a jQuery object).

serializeArray用于将包含表单的jQuery对象转换为结构化数据,作为将其转换为序列化字符串的前体。直接调用它很少有用,并且无法在数组上调用(otherData:basketData.serializeArray()}将抛出一个数组,因为serializeArray未定义,因为basketData不是jQuery对象)。

serialize is for converting a jQuery object containing a form into a string of form encoded data. It is great for dealing with complex forms. It becomes a little fiddly when you want to modify the data.

serialize用于将包含表单的jQuery对象转换为表单编码数据的字符串。它非常适合处理复杂的表格。当您想要修改数据时,它会变得有点繁琐。

Forget about using either of them for this.

忘记使用它们中的任何一个。

The data argument for .ajax accepts a JavaScript object as its value, and then jQuery will encode it as form data using PHP's conventions for complex data structures (which works out nicely for you since you say your array isn't flat and you are using PHP).

.ajax的数据参数接受一个JavaScript对象作为其值,然后jQuery将使用PHP的复杂数据结构约定将其编码为表单数据(由于您说您的数组不是平的并且您正在使用它,因此很适合您PHP)。

Just put the array and the value of the form control into your data object.

只需将数组和表单控件的值放入数据对象中即可。

data: {
    email: $("input[name='user-mail']").val(),
    array: basketData
},