如何通过AJAX-Post传递嵌套的PHP-Array?

时间:2022-10-16 13:21:45

I have a PHP-Script that expects:

我有一个PHP脚本,期望:

$_REQUEST['asdf']['bsdf']['csdf']

to be set when it is called, and I cannot change this. I want to call this PHP-Script via ajax:

当它被调用时被设置,我无法改变它。我想通过ajax调用这个PHP脚本:

myurl = 'example-url.com';
myid = $(this.$element).attr('id'); //gets the id of a textfield: csdf
value = 'somevalue';

$.ajax({type: 'POST',
        url: myurl,
        data: {'asdf[bsdf][myid]': value},
        success: function (data) {
          /* blabla */
       }
});

It does not work though, because the var "myid" is not filled with the supposed value "csdf". Can someone help me how to do this?

它不起作用,因为var“myid”没有填充假定的值“csdf”。有人可以帮我怎么做?

PS: I cannot access the PHP-File, so no changes in structure possible...

PS:我无法访问PHP文件,所以结构可能没有变化......

2 个解决方案

#1


0  

Try something like this:

尝试这样的事情:

var myurl = 'example-url.com';
var myid = $(this.$element).attr('id'); //gets the id of a textfield: csdf
var value = 'somevalue';

var param = {};
param[myid] = value;

$.ajax({type: 'POST',
        url: myurl,
        data: {'asdf[bsdf]' : param},
        success: function (data) {

                 }
});

You can build the param object outside the data you pass via the ajax call to keep the correct key in place.

您可以在通过ajax调用传递的数据之外构建param对象,以保持正确的密钥。

#2


0  

in JS

... {
     ...
     data: {[['first', 'array', 'nested'], 'other', 'in', 'parent', 'array']},
     ...
}

In PHP getting the POST vars is with other sentence

在PHP中获取POST变量与其他句子

http://php.net/manual/en/reserved.variables.post.php

#1


0  

Try something like this:

尝试这样的事情:

var myurl = 'example-url.com';
var myid = $(this.$element).attr('id'); //gets the id of a textfield: csdf
var value = 'somevalue';

var param = {};
param[myid] = value;

$.ajax({type: 'POST',
        url: myurl,
        data: {'asdf[bsdf]' : param},
        success: function (data) {

                 }
});

You can build the param object outside the data you pass via the ajax call to keep the correct key in place.

您可以在通过ajax调用传递的数据之外构建param对象,以保持正确的密钥。

#2


0  

in JS

... {
     ...
     data: {[['first', 'array', 'nested'], 'other', 'in', 'parent', 'array']},
     ...
}

In PHP getting the POST vars is with other sentence

在PHP中获取POST变量与其他句子

http://php.net/manual/en/reserved.variables.post.php