0

What could be causing my array from not forming correctly? I get the following result.

array(1) {
   ["element[119"]=>
   array(1) {
     ["data"]=>
     string(1) "0"
   }
 }

When it the result should look like this.

array(1) {
  ["element"]=>
  array(1) {
    ["119"]=>
    array(1) {
      ["data"]=>
      string(1) "0"
    }
  }
}

Simplified front end:

<input class="custom-control-input data is-valid" data-parent="0" data-qid="119" name="element[119][data]" id="119-Yes" type="radio" onchange="showTextBox(this)" value="1" checked="">

<script type="text/javascript">
var formData = {};
          $(form).find(":input.data:visible, input[type=hidden].data").each(function (index, node) {
              formData[node.name] = node.value;
          });
          console.log(formData);
$.ajax({
            url: "index.php?route=form/form/saveSection",
            data: { form_id: $("#formRequest").data("formsaveid"), section_id: $("#formRequest").data("currentsectionid"), path: $("#formRequest").data("formpath"), action: "saveSection", data: formData},
            dataType: "json",
            method: "POST",
            beforeSend: function() {
              console.log("Saving Section...");
              console.log("Section ID: "+$("#formRequest").data("currentsectionid"));
              console.log(postData);
            },
            success: function(data) {
            }
          });
</script>

Simplified back end:

var_dump($data);exit;

2 Answers 2

1

The issue is happening with the way you manipulate the data with

$(form).find(":input.data:visible, input[type=hidden].data").each(function (index, node) {
      formData[node.name] = node.value;
});

really you need to serialize the entire form and convert it to json, so replace the above with this:

var formData = JSON.parse($(form).serializeArray());

Then you can:

<?php
    var_dump($_POST['data']);
Sign up to request clarification or add additional context in comments.

10 Comments

That way isn't dynamic. My way I can add infinite depth to the array. It has something to do with post call from php. The json data saved into the post is {element[119][data]:"0"}
Your way doesn’t add infinite depth because it is setting your name keys incorrectly. What you actually need is to recursively look at these brackets in the field name and create a new depth if you need it to be dynamic. My example here just solves the problem you posed. I’ll re-do this shortly
Okay. It pretty much needs to emulate what happens when submitting a form. Instead using ajax. I would have assumed the post data looks exactly the same (name: value), but obviously doesn't since it isn't working.
I've updated the answer which should now solve your problem, sorry for the delay.
Have you tested my way? The reason why it is breaking is because your loop isn’t doing what you think it is.
|
0
name="[element][119][data]"

Try to encapsulate everything inside array brackets in your name tag in HTML.

1 Comment

It just moves where this bracket issue is happening.array(1) { ["[element"]=> array(1) { [119]=> array(1) { ["quantity"]=> string(1) "0" } }

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.