wood1y
wood1y

Reputation: 189

Send object with $http post in angular to a PHP array key=>value

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

)

Upvotes: 2

Views: 1009

Answers (1)

jedrzej.kurylo
jedrzej.kurylo

Reputation: 40909

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.

Upvotes: 1

Related Questions