-1

i use the wordpress built-in ajax implementation in one of my plugins as follows:

jQuery('#form_data_voting').submit(function() {
        jQuery.post(
            ajax_object.ajax_url,
            {
                action : 'voting_save',
                value : jQuery('#form_data_voting').serialize(),
            },
                function(data) {
                    alert(data);
                }
        );//get
});

And the function in PHP looks like follows:

add_action('wp_ajax_voting_save', function () {
    var_dump($_POST['value']);          
    die();
});//wp_ajax_voting_save

I then see in the dump that all relevant fields are in one string - thanks to the serialize() function. But the problem here is, that it is stored behind the "value" variable and i can't get it out there. The string looks like this: name=testname&tel=3883838. I can't access the name via: $_POST['name'] or $_POST['value']['name'].

How can i access name and tel as POST-variables in the wordpress ajax environment or is that not possible and i have to create name and tel as values in the js-file and have to leave serialize() out of the function?

Would be great if anybody has experiences this before and can share the solution. Thanks!

Torben
  • 5,388
  • 12
  • 46
  • 78

2 Answers2

5

thanks VukBG - for my example not parse_url but parse_str is sufficient:

    parse_str($_POST['value'], $my_array_of_vars);
    print_r($my_array_of_vars);
Torben
  • 5,388
  • 12
  • 46
  • 78
0

Try parse_url() function. I think that is what you are looking for.

You can also see a good example for this function here

Community
  • 1
  • 1
Vuk Stanković
  • 7,864
  • 10
  • 41
  • 65