2

I'm trying to send an Object to PHP wit POST and convert it to an associative array

    postData: function(word, description, translate) {
        var formData = {
            w: word, 
            d: description, 
            t: trasnlate
        };

        $http({ 
            method: 'POST', 
            url: 'db.php', 
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            data: formData
        }).
            success(function(data, status, headers, config) {
                console.log(data);
            }).
            error(function(data, status, headers, config) {

            });
    }

db.php

print_r($_POST); 

console.log(data):

 Array(
[{"w":"word","d":"description","h":"translate"}] => )

and I want something like this:

 Array(
[w] => word
[d] => description
[t] => translate

)

1
  • What do you get from the print_r($_POST); ? Commented Jul 4, 2015 at 13:17

1 Answer 1

1

In your PHP code do:

$json = file_get_contents('php://input');
$array = json_decode($json, true);

$array will be an associative array if proper JSON object was provided in POST payload.

Sign up to request clarification or add additional context in comments.

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.