使用AJAX将两个不同的jQuery数组传递给php(普通数组和多维数据)

时间:2022-05-27 02:09:33

I'm using ajax to do calculations as people fill out a form. I'm trying to figure out how to pass 2 different types of arrays with AJAX and do stuff with them in PHP. First array is just ID's, 2nd array is multidimensional.

当人们填写表格时,我正在使用ajax进行计算。我正在试图弄清楚如何使用AJAX传递2种不同类型的数组,并在PHP中使用它们。第一个数组只是ID,第二个数组是多维的。

"members" array is from jQuery Chosen field which creates arrays of all the member ID's such as ["1", "1116210"].

“members”数组来自jQuery Chosen字段,它创建所有成员ID的数组,例如[“1”,“1116210”]。

var members = [];
members.push(id);  // loop through and push ID to "members" array

This works when i send it like

当我发送它时,这是有效的

data: { "data": members }

Second array I don't know how to pass too, is multidimensional from 4 different input boxes so it looks something like this in jQuery.

第二个数组我不知道如何传递,是4个不同的输入框的多维,所以它在jQuery中看起来像这样。

nonMembers[0]['name']
nonMembers[0]['email']
nonMembers[0]['rating']
nonMembers[0]['gender']

Here's my ajax call to a PHP file

这是我对PHP文件的ajax调用

$.ajax({
        url:url,
        data: { "data": members },
        beforeSend: function() {
            $('.MembersPostback').html('<div class="loading"><img src="/images/loading.gif" /></div>');
        },
        success: function(response){ 
            $('.MembersPostback').html(response);    
        },
        error: function(xhr, status, error) {
            alert(xhr.responseText);
        }
    });

1 个解决方案

#1


2  

You can pass it like

你可以传递它

data: { members_data: members , nonMembers_data : nonMembers},

and in php

在PHP中

<?php
  $members = $_GET['members_data'];
  $nonMembers = $_GET['nonMembers_data'];
?>

#1


2  

You can pass it like

你可以传递它

data: { members_data: members , nonMembers_data : nonMembers},

and in php

在PHP中

<?php
  $members = $_GET['members_data'];
  $nonMembers = $_GET['nonMembers_data'];
?>