0

I try to post via ajax some data. Watch sreenshot1. But jQuery automatic converts object to array. This can be seen in screenshot2 (chrome developer tools).

Data send to $.post:

enter image description here

Data inspect in chrome developer tools:

enter image description here

Edited:

Problems appear in PHP where I expect for Data property to be array of objects:

enter image description here

I want to read data like this:

foreach ($_POST["Data"] as $field)
{
    if ($field->Field == "username")
    {
            ...
    }
}
Patrick M
  • 10,547
  • 9
  • 68
  • 101
Makla
  • 9,899
  • 16
  • 72
  • 142
  • 3
    It *is* an array of objects. More specifically it's an object containing an array of objects as one of its properties. – Grinn Jul 23 '14 at 18:26
  • for this kind of question it can help to explain what output you are actually trying to obtain. – njzk2 Jul 23 '14 at 18:26
  • 3
    *"Problems appear in PHP where I expect for Data property to be array of objects"* Your objects will be associative arrays in PHP. Is that a problem? Alternative you can encode the whole array as JSON. – Felix Kling Jul 23 '14 at 18:27
  • What exaclty does your PHP expect the request to look like? – Bergi Jul 23 '14 at 18:30
  • 1
    Not an answer, but for the line above, `options.Parameters = options.Parameters || {}` is shorter, and possibly more efficient. – Scimonster Jul 23 '14 at 18:37
  • I expect that data send to server is exactly that Grinn formatted in his answer. I added screenshot3 in my post. – Makla Jul 23 '14 at 19:02
  • 1
    You can change `$field->Field ` to `$field['Field']` and the problem would be solved. Or as I said, encode the data as JSON. – Felix Kling Jul 23 '14 at 19:48
  • Thanks. I thought that jQuery does that. Javascript: { Operation: "Add", TableName: this.TableName, Data: JSON.stringify(d) } PHP: json_decode($_POST["Data"]) I thought this is done automatically. – Makla Jul 23 '14 at 19:57
  • `json_decode` is a separate function from whatever piece of magic turns request (GET, POST, COOKIE) parameters into arrays. Request parameters are not JSON. If you added your `
    ` markup and how you're turning that into `options.parameters`, we might be able to figure out why it isn't serializing into a JSON object. [`$.post`](http://api.jquery.com/jquery.post/) takes an additional parameter of `datatype` that you could try changing to `json`.
    – Patrick M Jul 24 '14 at 13:38

2 Answers2

2

It's not really well spelled out in the PHP docs, but this is how it's supposed to work. For instance, the page on request variables has this to say about array-like cookies:

If you wish to assign multiple values to a single cookie variable, you may assign it as an array. For example:

<?php
  setcookie("MyCookie[foo]", 'Testing 1', time()+3600);
  setcookie("MyCookie[bar]", 'Testing 2', time()+3600);
?>

That will create two separate cookies although MyCookie will now be a single array in your script. If you want to set just one cookie with multiple values, consider using serialize() or explode() on the value first.

In other words, when you send in:

FormData[0][Field]: username
FormData[0][Value]: test3

PHP interprets this as an array of arrays, which looks like this:

array(1) {
  [0]=> array(2) {
    ['Field'] => String(8) "username",
    ['Value'] => String(5) "test3"
  }
}

And you can parse it like so:

foreach ($_POST["Data"] as $field)
{
    if ($field['Field'] == "username")
    {
            ...
    }
}

Bear in mind that a PHP array is actually an ordered map. In other words, its perfectly suited for storing the key => value data you're passing in with your POST.

You seem to be expecting $_POST to contain simple objects (aka stdClass) and it simply doesn't. Most SOAP helpers in PHP will give you simple object responses (which is a typical blending of behaviors from PHP). See this question for some ideas if you really want to translate every $_POST["Data"] field into an object. But that would be expensive and unnecessary.

Community
  • 1
  • 1
Patrick M
  • 10,547
  • 9
  • 68
  • 101
0

The answer is that you are in fact sending an array of objects - not an array of arrays as you've supposed. Here is an example of the data that you are sending to the server:

{
    Operations: "Add",
    TableName: "users",
    Data: [
        {
            Field: "username",
            Value: "test3"
        },
        {
            Field: "someOtherField",
            Value: "test4"
        },
        // ...
    ]
}

If on the PHP side you are expecting only an array of objects, and not an object containing an array of objects, then you should instead pass options.Parameters.Data, like:

var result = $.post(url, options.Parameters.Data, // ...
Grinn
  • 5,370
  • 38
  • 51