0

I'm stuck, I have googled and searched here on the stack.

First, I create a JS obj:

var formData = {};

//LOOP THREW TABLE ROWS
$("tr.element_row").each(function(index, element){

     var $this = $(this);
     var $inputs = $this.find("input.formData");

     formData[index] = {};

      //LOOP THREW INPUTS
      $.each($inputs, function(n, e){
         //this is each input in this tr

         if( $(this).attr('name') == 'el' ){
             formData[index]['el'] = $(this).val();
         } ...

Then I convert it with JSON stringify:

var myJSON = JSON.stringify(formData);

//RESULT (console.log(myJSON))
{"0":{"obj":"1234","el":"1","lit":"1","height":"","type":"","length":"","width":"","weight_kg":"","proj":"BC"},"1":{"obj":"1234","el":"2","lit":"1","height":"","type":"","length":"","width":"","weight_kg":"","proj":"BC"},"2":....

Then I send it with ajax to PHP:

$.ajax({
    url: 'php/add_elementdata.php',
    method: 'post',
    dataType: "json",
    data: myJSON,

Then i do a var_dump($_POST)

//RESULT
array(1) {
["{"0": {"obj":"1234","el":"1","lit":"1","height":"","type":"","length":"","width":"","weight_kg":"","proj":"BC"},"1":{"obj":"1234","el":"2","lit":"1","height":"","type":"","length":"","width":"","weight_kg":"","proj":"BC"},"2":{"obj":"1234","el":"3","lit":"1","height":"","type":"","length":"","width":"","weight_kg":"","proj":"BC"},"3":{"obj":"1234","el":"4","lit":"1","height":"","type":"","length":"","width":"","weight_kg":"","proj":"BC"},"4":{"obj":"1234","el":"5","lit":"1","height":"","type":"","length":"","width":"","weight_kg":"","proj":"BC"}}"]=> string(0) ""}

Then I want to create a multidimensional array of this.. array..string..

$arr = json_decode($_POST);
or
$arr = json_decode($_POST, true);
echo $arr;

But $arr returns empty. why?

8
  • 2
    var_dump($arr); not echo its an std object Commented Jan 30, 2020 at 9:23
  • @Rahul Well, if it had worked it had sad "array" but now echo says nothing and var_dump say "null" Commented Jan 30, 2020 at 9:25
  • Try data: {'myJSON': myJSON} instead of data: myJSON. And on server end print it by print_r($_POST) Commented Jan 30, 2020 at 9:27
  • $_POST returns array of all post vars. Try json_decode($_POST[0]); to only get the first array. Commented Jan 30, 2020 at 9:30
  • 1
    also you should be sending data: {'myJSON':myJSON} then getting it with $_POST['myJSON'] Commented Jan 30, 2020 at 9:34

3 Answers 3

1

You've passed data: myJSON,, it means that you've passed variable (your JSON string) with empty value. It was shown in var_dump() - ["json" => '']. So, you can use it in PHP in the next way:

 foreach($_POST as $data=>$emptyVal){
   // here you can add any checking stuff
    $arr = json_decode($data,true) ;
 }

print_r($arr);

Example

But, you need just to change data: myJSON, on, for example, data: {jsonn : myJSON},. And now you will be able to use it in PHP as $_POST['jsonn']:

$arr = json_decode($_POST['jsonn'],true) ;
Sign up to request clarification or add additional context in comments.

Comments

0

When you're sending an ajax request via jQuery and you want to send FormData you don't need to use JSON.stringify on this FormData.

and also when you're sending file the content-type must be multipart/form-data. so use something like this

$.ajax({
    type: "POST",
    url: "url",
    data: formData,
    processData: false,
    contentType: false,
    success: function(response) {
        console.log(response);
    },
    error: function(errResponse) {
        console.log(errResponse);
    }
});

4 Comments

Before i went with the solution to create a string, i was sending a object. But it did not work. The object was cut after 111 nestled indexes.. i send 199.. but only 111 reccived to the php file
Did you try with serialize form data?
I also wonder if JSON.stringify() is necessary, but 1) OP is not using FormData; 2) OP is not sending files. 3) contentType: false? What happened to the multipart/form-data recommendation?
Setting processData to false (prevent jQuery from automatically transforming the data into a query string), Setting the contentType to false (This is needed because otherwise, jQuery will set it incorrectly).
0

Try

$.ajax({ type: "POST", url: "url", data: {data: myJSON}, });

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.