I'd like to create a Multi-Level Associative Object on $.post method using WordPress, in order to make my code cleaner. The problem is, I don't know how to get those values to manipulate on the back-end.

This is what I would like to do in my_query.js file, which contains and handles the AJAX request.

$.post( MyAjax.ajaxurl, {
    action: 'my_action',
    someValue: {
        one: 'Some Value One',
        two: 'Some Value Two',
        three: 'Some Value Three',
        four: 'Some Value Four',
    }
} );

And this is the current solution, which I DO NOT want to implement on my code.

$.post( MyAjax.ajaxurl, {
    action: 'my_action',
    someValueOne: 'Some Value One',
    someValueTwo: 'Some Value Two',
    someValueThree: 'Some Value Three',
    someValueFour: 'Some Value Four'
} );

The only way I could get those values on the back-end is by using the second option and doing this in the .php file.

$someValue = array(
    'one'   => $_POST[ 'someValueOne' ],
    'two'   => $_POST[ 'someValueTwo' ],
    'three' => $_POST[ 'someValueThree' ],
    'four'  => $_POST[ 'someValueFour' ]
);

echo $someValue[ 'one' ];

wp_die();

But... What is the problem anyways?

I DO NOT know how to have access to the AJAX Data Object inside my .php file by using the first example (which I'd like to), it only works by using the second one. How could I return the someValue object inside the .php file by using the first AJAX Request example?

  • when you try the associative array, what do you receive in $_POST ? – mmm Mar 16 '16 at 11:40
  • @mmm I receive the value passed in the Data Object inside the $.post method. In this case, I defined Some Value One in the someValueOne property, and as expected, I'm getting a 200 as Response Status and the property value perfectly. – iszwnc Mar 16 '16 at 11:46
  • @mmm I just noticed that if I run return it shows me 0 instead of the actual value, so I changed to echo. – iszwnc Mar 16 '16 at 12:02
  • you said that you receive the value so what is the problem ? please explain whit more details, show what you get and explain what you wish to get – mmm Mar 16 '16 at 12:39
  • @mmm Oh I thought you were asking what I was getting from using the second option not the first one. Well if you referred to the first one, I was that hoping you guys could tell me LoL, because I don't know if it's possible to access to data structured like that. I actually don't know how to manipulate the data from the Data Object on the Back-End. Do you got me? – iszwnc Mar 16 '16 at 17:40

If you want to pass a complex value, your best path is probably to jsonify it. Something like

$.post( MyAjax.ajaxurl, {
    action: 'my_action',
    someValue: JSON.stringify({
        one: 'Some Value One',
        two: 'Some Value Two',
        three: 'Some Value Three',
        four: 'Some Value Four',
    })
} );

and then on the server side you decode the value with

$someValue = json_decode($_POST['someValue']);

At this point $someValue should be the array you wanted to pass.

  • Have you tested it? Just asking, because it didn't work, when I run echo $someValue[ 'one' ] it shows me nothing and when I run return it shows me 0. – iszwnc Mar 16 '16 at 12:27
  • 1
    @iszwnc, obviously I have not tested it, answers on SE in general should be taken as a concept not as code you can assume that you can copy and paste.and it will work (although I will fix the answer if you find out what is wrong ;) ) – Mark Kaplun Mar 16 '16 at 12:30
up vote 0 down vote accepted

Actually the solution I've found was very simple and didn't required much. For some reason I wasn't able to retrieve the data inside my AJAX Function Request on the Back-end.

JavaScript Code

As you can see below I defined the someValue property just the way I wanted, very clean and pretty.

$.post( MyAjax.ajaxurl, {
    action: 'my_action',
    someValue: {
        one: 'Some Value One',
        two: 'Some Value Two',
        three: 'Some Value Three',
        four: 'Some Value Four'
    }
} );

PHP Code

I must've forgot that I could associate all the properties defined inside the Data Object as an Array inside my Back-end function.

$someValue = array(
    'one'   => $_POST[ 'someValue' ][ 'one' ],
    'two'   => $_POST[ 'someValue' ][ 'two' ],
    'three' => $_POST[ 'someValue' ][ 'three' ],
    'four'  => $_POST[ 'someValue' ][ 'four' ]
);

wp_die();

If you are unsure about what kind of data you're getting from the AJAX Request. I recommend you to do what our friend @mmm asked me, just var_dump() or print_r() the $_POST superglobal, then you'll see if you're actually getting something.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.