Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs $http.post, passing array to PHP

Tags:

php

angularjs

I am using a service to update a DB table.

myApp.factory('createGal', function ($http, $q)
{
    return {
        createGal: function ()
        {
            var deferred = $q.defer();
            var newGalleryArray = {};

            newGalleryArray.galleryName = 'New Image Gallery';
            newGalleryArray.client      = 245;

            $http.post('/beta/images/create', {newGalleryArray: newGalleryArray}).success(function(data)
            {
                console.log(data);
                deferred.resolve(data);
            });

            return deferred.promise;
        }
    };
});

PHP

public function create()
{
    print_r($_POST);
}

The array is returning empty. Am i passing the array incorrectly?

Chrome Dev enter image description here

Thanks

like image 453
SuperNinja Avatar asked Jul 31 '13 15:07

SuperNinja


1 Answers

It's been a while since I've used PHP, but doesn't $_POST just contain request paramaters? $http.post sends data through a JSON payload, not request parameters. So, you'll need to use something like json_decode

like image 151
dnc253 Avatar answered Sep 19 '22 05:09

dnc253