0

I want to pass data from angularjs to wordpress using AJAX

In my angular code i have this data:

$scope.registrations.push({ id_associate: '1', activity:'demo1', qty:"1" });
$scope.registrations.push({ id_associate: '2', activity:'demo2', qty:"15" });
$scope.registrations.push({ id_associate: '3', activity:'demo3', qty:"5" });

And then i call an AJAX method in this way:

$http({
        method : 'POST',
        url: '/wp-admin/admin-ajax.php', 
        params: {
            action: 'registration', 
            fn: 'registerUsers', 
            registrations: $scope.registrations, //???????          
        }
      }).success(function(data) {
            console.log( 'sucess data' );
});

Then in WordPress i have the function for using the data:

function registration_ajax() {
        global $current_user;
        get_currentuserinfo();
        switch ( $_REQUEST['fn'] ) {
            case 'registerUsers':
                $registrations = ?????;
                foreach ( $registrations as $reg ){
                    self::saveRegistration( 
                        $reg->id_associate, 
                        $reg->activity, 
                        $reg->qty, 
                        $current_user->ID 
                    );                  
                }
            break;
        }
    }

But I don't know how to pass the data from angularjs and then receive the data in the Wordpress side.

How should I pass the data?

Oterox
  • 642
  • 1
  • 10
  • 27
  • have you added your actions to wp_ajax ? – David Feb 21 '16 at 23:08
  • Sure, the ajax is working for several methods i call from jquery. But i've changed to angularjs and i don't know how to make it work. – Oterox Feb 21 '16 at 23:10
  • The original method (working with jquery) was "registerUser" which gets 4 parameters: id_associate, activity, qty and current_user. Now it recieves a users array ($scope.registrations) – Oterox Feb 21 '16 at 23:12
  • you should have a post field called registrations, if you var_dump it and console log the returned data you should see what format it has taken. Its been a while since i worked with ajax but as far as i remember there is support for levels of assoc arrays so it should be registrations[id] etc or else you have a string... – David Feb 21 '16 at 23:21
  • See http://stackoverflow.com/a/32507103/1193038 – Pablo S G Pacheco Jun 10 '16 at 02:33

1 Answers1

0

In AngularJs

JSON.stringify($scope.registrations);

In PHP

 $registraions =  json_decode($_REQUEST['registrations']);
Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
Scott
  • 805
  • 7
  • 19
  • Can you explain why and how this solves the question? See [how to write a good answer](http://stackoverflow.com/help/how-to-answer) – jkalden Feb 01 '17 at 13:13
  • You're simply serializing the object so It can be passed via http. After the request is complete you're deserializing<-if thats a word, to utilize in php. But to be frank, Its a bit self explanatory. – Scott Feb 02 '17 at 06:40