1

I got stuck on working with my server and don't find a solution for now. I hope you can help me.

I have a service from where I want to send a POST request. The service looks like: (I want to use a service, because I need to call this from different sites)

angular.module('schwimmfestivalApp')
    .service('groupService', function($http) {
        // AngularJS will instantiate a singleton by calling "new" on this function
        var url = '../../../server/func/getGroups.php';
        var createGroupUrl = '../../../server/func/setGroup.php';
        var groupCategoryUrl = '../../../server/func/getGroupCategory.php';


        this.getGroups = function() {
            return $http.get(url, {cache: false}).then(response => {
                console.log('Groups: ' + JSON.stringify(response.data));
                return response.data;
            });
        };

        this.createGroup = function(group) {
            return $http.post(createGroupUrl, {'group': group}, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}}).then(response => {
                console.log('Group to Create: ' + group);
                console.log('Resonse: ' + JSON.stringify(response));
                console.log('Create Group: ' + response.data);
                return response.data;
            });
        };

    });

The getGroups() Method is working fine.

My Server code (not written by me) for the setGroups() Method looks like:

<?php

require_once '../connect/connect_db.inc';
require_once '../class/gruppe.php';
require_once '../func/getHeader.php';

$db = getConnection();
getHeader('json');

if(isset($_POST['group'])){

    $idGruppe = 0;
    //$gruppeJson = json_decode(file_get_contents("php://input"));
    $gruppeJson = json_decode($_POST['group']);

    $sql = "INSERT INTO gruppe ";
    $sql .="( name, gruppe_typ_id) VALUES ('";
    $sql .= $gruppeJson->{'name'} . "', '";
    $sql .= $gruppeJson->{'idKategorie'};
    $sql .=" ')";

    if ( $db -> query($sql)) {
        $idGruppe = $db -> insert_id ;

        $selectGroup = "SELECT gruppe.id, gruppe.name, gruppe.gruppe_typ_id AS idKategorie, gruppe_typ.name AS kategorie ";
        $selectGroup .= "FROM gruppe, gruppe_typ ";
        $selectGroup .= "WHERE gruppe.gruppe_typ_id = gruppe_typ.id AND gruppe.id = " . $idGruppe;
        if ($result = $db -> query($selectGroup)) {
            while($row = $result->fetch_assoc()) {
                $gruppe = new gruppe();
                $gruppe->id = $row['id'];
                $gruppe->name = $row['name'];
                $gruppe->idKategorie = $row['idKategorie'];
                $gruppe->kategorieName = $row['kategorie'];
            }

            echo json_encode($gruppe);
        } else {
            echo $db->error;
            echo "</br> " . $selectGroup;
        }
    } else{

    }
}

I have read something about you have to read the data with file_get_contents. But I don't get there. The if(isset($_POST['group'])) doesn't validate to true.

So how do I have to build my Request to get into this if ? Do you have any suggestions for me?

Thanks in advance Dominic

dominic
  • 385
  • 1
  • 6
  • 23
  • Display `var_dump($_POST)` in your php file and check response in firebug when you call post from angular. – lukassz Mar 27 '16 at 22:52
  • Ok I did as you said now I get alway an empty array or JSON, relying on wich header I set. – dominic Mar 27 '16 at 23:54

2 Answers2

1

You'll be able to get whatever data is posted through using the following: file_get_contents("php://input"). This will return a json string that you'll be able to call json_decode on in order to get a key/value object.

If you do want to use $_POST, you'll have to call $.param on your data before you pass it into $http.post. This will serialise your data and as such, PHP will create the $_POST key/value pair.

Thomas Maddocks
  • 1,355
  • 9
  • 24
  • I tried to var_dump(file_get_contents("php://input") too. But it seems to be empty too. Do I have to inject $ to my service? – dominic Mar 28 '16 at 06:26
  • `$.param` is the same as `jQuery.param` so all you'll have to do is make sure you include the jQuery library in your page. – Thomas Maddocks Mar 28 '16 at 07:07
0

Thanks to all for your hints.

For now we used the standard way angular sends Data to the server.

so at the server side we do $gruppeJson = json_decode(file_get_contents("php://input")); and then parse it to JSON.

You will find a good explanation here angularJs POST data in $_POST for php

Community
  • 1
  • 1
dominic
  • 385
  • 1
  • 6
  • 23