4

I use AngularJS to build an AJAX form which is then processed on a server running Wordpress.

Server-side handler is simple:

function rnr_contact_callback() {
    $name = $_POST['firstName'] . ' ' . $_POST['lastName'];

    wp_mail(
        '[email protected]',
        'Contact form submitted',
        $name . '(' . $_POST['email'] . ') sent a message: ' . $_POST['comment']
    );

    exit;
}

Client-side controller:

angular.module('app').controller('ContactForm', function($scope, $http) {
    $scope.sendContactForm = function() {
        $http({ 
            method: 'POST', 
            url: '/wp-admin/admin-ajax.php', 
            params: {
                action: 'contact',
                firstName: $scope.userFirstName,
                lastName: $scope.userLastName,
                email: $scope.userEmail,
                comment: $scope.userComment
            }
        }).success(function(data, status, headers, config) {
            $scope.contactFormSent = true;
        }).error(function(data, status, headers, config) {

        });
    };
});

I tested the $scope consistency, and it is all right. Moreover, the POST request is handled: I receive an email. The problem is, email body looks like: () sent a message:. I make a conclusion that, on server, none of $_POST['...'] is set. Why? What do I do wrong?

1
  • curl -X POST http://104.131.108.xx/wp-admin/admin-ajax.php --data '{"action":"rate","shop":1,"rating":5}' --header "Content-Type: application/json" returns 401 Unauthorized. I'm completely sure I allowed unauthorized users to send POST requests.
    – rishat
    Oct 14, 2014 at 0:08

4 Answers 4

4

I got it. I just should have used $_REQUEST instead of $_POST within every function which handles AJAX requests.

3
  • Additional infos as edit to your question, please. Also: Try $_GET ;) and about how to handle AJAX in WordPress.
    – kaiser
    Oct 14, 2014 at 0:47
  • $_GET was empty, too, to my very surprise. I'm 100% sure I sent a POST request (used $http module from AngularJS). Although, I'm still confused about how this works in Wordpress and, moreover, in PHP generally.
    – rishat
    Oct 14, 2014 at 19:32
  • Follow the link in my comment above. I've written that part to answer exactly that question :)
    – kaiser
    Oct 14, 2014 at 20:37
3

Parse action:

function angular_ajax_params_to_post() {
    $is_ajax = ( defined( 'DOING_AJAX' ) && DOING_AJAX );
    if($is_ajax){
        $params = json_decode(file_get_contents('php://input'), true);

        foreach ($params as $param_key => $param_val) {
            $_POST[ $param_key ] = $param_val;
        }
    }
}
add_action( 'plugins_loaded', 'angular_ajax_params_to_post' );

This function parse angular post string into $_POST array

2

Just had the same issue. The $_REQUEST is empty because angulars $http sends its data as JSON per default. Yu have to send the data as formdata:

var app = angular.module('foo', []);

app.config(function ($httpProvider) {
    // send all requests payload as query string
    $httpProvider.defaults.transformRequest = function(data){
        if (data === undefined) {
            return data;
        }
        return jQuery.param(data);
    };

    // set all post requests content type
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
});
2
  • I don't like to use jQuery inside angular so i used this great Ben Nadel solution, it is the same as using jQuery bennadel.com/blog/… sadly it does not solved my worpdress ajax / angular $http problems :(
    – Magico
    Jun 10, 2016 at 10:21
  • You saved my day Feb 4, 2017 at 21:37
0

You have to use $_REQUEST instead of $_POST in PHP:

function rnr_contact_callback() {
    $name = $_REQUEST['firstName'] . ' ' . $_REQUEST['lastName'];

    wp_mail(
        '[email protected]',
        'Contact form submitted',
        $name . '(' . $_REQUEST['email'] . ') sent a message: ' . $_POST['comment']
    );

    exit;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.