我有一个用php编码的对话框。提交后,我将数据发送回对话框以通过ajax存储(我不希望对话框关闭)。这通常可以正常工作,但是发布的数据格式不是我想要的。这是ajax代码
$('#but1').click(function() {
var apply_to = $("input[name='group-apply']:checked").val();
var el = document.getElementById('multi-selector');
var selected = GetSelectValues(el);
var data = [];
data[0] = 'apply-to:'+ apply_to;
data[1] = 'selected:' + selected;
data = {data};
$.ajax({
url : "example.php",
type: "POST",
data : data,
success:function(result){
$("#form-container").html(result);
}
});
});
运行以上命令后,对话框中的php会显示此
Array
(
[data] => Array
(
[0] => apply-to:Include
[1] => selected:8,9,5,1
)
)
为了使用上面的代码,我需要像这样访问它,然后将其爆炸以获取值。
$_POST['data'][0]
我希望数据像这样显示
Array
(
[0] => apply-to:Include
[1] => selected:8,9,5,1
)
或者,理想情况下:
Array
(
[apply-to] => Include
[selected] => 8,9,5,1
)
如果可以的话,请解释一下该怎么做?
答案 0 :(得分:0)
在您的php代码中,您可以将$ _POST ['data']关联到数组
$myArray = $_POST['data'];
然后$ myArray应该为
Array
(
[0] => apply-to:Include
[1] => selected:8,9,5,1
)
答案 1 :(得分:0)
尝试使用
$('#but1').click(function() {
var apply_to = $("input[name='group-apply']:checked").val();
var el = document.getElementById('multi-selector');
var selected = GetSelectValues(el);
var data = [];
data['apply-to'] = apply_to;
data['selected'] = selected;
data = {data};
$.ajax({
url : "example.php",
type: "POST",
data : data,
success:function(result){
$("#form-container").html(result);
}
});
});
答案 2 :(得分:0)
您需要将data
设置为对象{}
获得价值
Array
(
[apply-to] => Include
[selected] => 8,9,5,1
)
使用
var data = {};
data['apply-to'] = apply_to;
data['selected'] = selected;
//data = {data}
并获得
Array
(
[0] => apply-to:Include
[1] => selected:8,9,5,1
)
使用
var data = {};
data[0] = 'apply-to:Include';
data[1] = 'selected:8,9,10';
//data = {data}