I have two simple scripts. One client-side jquery that has multidim array & a server-side php script. in php $data stays empty.
jquery
console.log("IN JQUERY");
console.log(inps);
$.ajax({
type: 'post',
cache: false,
url: './gen.php',
data: inps,
success: function(response){
console.log("RESPONSE");
console.log(response);
}
});
gen.php
<?php
$data = file_get_contents('php://input');
$data = json_decode($data, true);
print_r($data);
?>
firefox console output
>POST ..././gen.php [HTTP/1.1 200 OK 1ms]
>"IN JQUERY"
>{isbranch: "1", ismanager: "0", isdept: "1"}
>"RESPONSE"
>""
Is there a way to send Multi Dimensional array to the php with ajax without spliting array?
asked Jun 10, 2014 at 22:22
user2102266
5393 silver badges15 bronze badges
2 Answers 2
You should use JSON.stringify to encode your data before send it, also better to add correct contentType to it:
$.ajax({
type: 'post',
cache: false,
url: '/gen.php',
data: JSON.stringify(inps),
contentType: 'application/json',
success: function(response){
console.log("RESPONSE");
console.log(response);
}
});
answered Jun 10, 2014 at 22:34
Bogdan Savluk
6,3121 gold badge32 silver badges37 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
user2102266
...perfect "IN JQUERY" test:515 {isbranch: "0", ismanager: "1", isdept: "0"} test:516 "RESPONSE" test:525 "Array ( [isbranch] => 0 [ismanager] => 1 [isdept] => 0 ) "
The key-value pairs should already exist in your $_POST
$isbranch = $_POST['isbranch'];
$ismanager = $_POST['ismanager'];
$isdept = $_POST['isdept'];
answered Jun 10, 2014 at 22:39
Fabricator
12.8k2 gold badges29 silver badges40 bronze badges
Comments
default
var_dump($_POST);?