0

So, guess what: I'am using AngularJs's $http to send data to my PHP using the $_POST method with all my data stored in an object i will give as a parameter to $http.

What I'am trying to achieve is to recover my ARRAY sended trought $_POST, in my php then properly use it in a foreach loop.

Also I'am using $http like the following:

//Initializing my data array.
$scope.myDataArray = ['potato', 'Batman', 'Monkey'];


$scope.iHaveNoName = function(){
   //action name
    var actionVar = "iHaveNoName";

    //Define callback function where "resp" is what my php return to me 
    //after sending the sql request and "parsing" it.

    var callback = function(resp){

        if(resp.success == 1){

            //Do something in case of success

        }else{
            $scope.$emit('showAlert', ["An error as occured","error"]);

        }

    };

    //Sending data trought $http using the object "way", that makes sense to me.
    var params = {
        'action' : actionVar,
        'idBigouder' : $scope.bigouder.idUser,

        //also passing the data array
        'data' : $scope.myDataArray
    };

    //HTTP REQUEST
    $http.post(url, params)
    .success(callback)
    .error(function(){
        $scope.$emit('showAlert', ["Error","error"]);

    });
};

So, my big problem is that when I try to send an array like the one I defined at the begining (myDataArray) it seems like it didnt send nothing, I mean that when I try to recover my array in my php page using $_POST['data'] it send me an error (that's not from the else, it means that it's my PHP who throws me an error), also i try to do a foreach to recover the data inside my array and then use it like that: foreach ($_POST['data'] as $cell) .

If you need more just ask, it didnt make sense for me to add the PHP as long as I've no more to show you than that, but if you want it, you are free to ask, thank you all :).

(obviously french baguette right here, be free to correct my grammar/speeling/others)

N.K
  • 1,601
  • 2
  • 19
  • 31
  • @Havelock i'am using a very different way, that's not going to solve my problem as long as this guy don't have the same goal as me, thanks for trying anyway. – N.K Oct 12 '16 at 10:18
  • This is a duplicate. The best answer IMHO is [this one](http://stackoverflow.com/a/30582142/283366) – Phil Oct 12 '16 at 10:57
  • @Phil that's an interesting answer, but definitively not what i'am searching for :/ i'am trying to send an Array trought $_POST then recover and use it inside a foreach, so even if i will look what he was talking about, this guy didnt give me any hints too. I will Edit my question, seems like she really need it. – N.K Oct 12 '16 at 11:03
  • 1
    Urgh, you're not even reading it are you? You need to set the content-type to `application/x-www-form-urlencoded` and serialize the data (`params` in your code) via `$httpParamSerializer`. Otherwise, Angular is sending a raw JSON request body which you can only read in PHP via `json_decode(file_get_contents('php://input'))` – Phil Oct 12 '16 at 11:06
  • @Phil -> hum, i wasn't looking at the right one xD, thank you for your time, i will try this :)! Also another way i just found is to use `toString` property on the array to send it, then just "decode" using php `explode` function :) – N.K Oct 12 '16 at 11:10

0 Answers0