1

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?

2
  • What do you get from var_dump($_POST);? Commented Jun 10, 2014 at 22:23
  • @kingero exact console-output : "<pre class='xdebug-var-dump' dir='ltr'><font color='#3465a4'>null</font> </pre>" Commented Jun 10, 2014 at 22:28

2 Answers 2

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);
  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

...perfect "IN JQUERY" test:515 {isbranch: "0", ismanager: "1", isdept: "0"} test:516 "RESPONSE" test:525 "Array ( [isbranch] => 0 [ismanager] => 1 [isdept] => 0 ) "
0

The key-value pairs should already exist in your $_POST

$isbranch = $_POST['isbranch'];
$ismanager = $_POST['ismanager'];
$isdept = $_POST['isdept'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.