How to pass data from Angular to a php class?

Asked

Viewed 397 times

1

I have the following code in Angular:

 <script>
 angular.module("fluxo", ["ngRoute"]);

.factory('factCliente', ['$http', function($http) {
    var _getData2 = function(id_empresa) {
    return $http.post("php/index.php", id_empresa);
};

return {
    getData2: _getData2
}
}])

.controller("fluxoCtrl", function ($scope, $http, factCliente) {

    var id_empresa = {id_empresa: 1};
    factCliente.getData2(id_empresa).then(function(response) {
        $scope.mostraTodasContasEntradas = response;
    }, function(error) {
        console.log("Ocorreu um erro: " + error);
    });
});
</script>

And my php code that includes the class and calls the proper function I want to call

<?php
 require_once "../con/conexao.php";
 require_once "../classes/contaEntrada.php";

 $entrada = new contaEntrada();

 $payload = json_decode(file_get_contents('php://input'));
 $id_empresa = $payload->id_empresa;

 $id_empresa = $_POST['id_empresa'];

 $entrada->mostraContasEntrada($id_empresa);
?>

I know the error is here, in this code in php, but I do not know where. Can someone help me?

  • where the $_POST is?

  • Sorry, I had already adjusted from $_GET to $_POST, but had not updated @Marcelobonus.

1 answer

1


Posts at the angle are shot as Request Payload, so on the PHP side you can get them that way

$payload = json_decode(file_get_contents('php://input'));
$id_empresa = $payload->id_empresa;

Also remove the ; from line 2 of the script and in your controller set the company id variable as follows

var id_empresa = {id_empresa: 1};

full code working:

Script:

<script>
 angular.module("fluxo", ["ngRoute"])

.factory('factCliente', ['$http', function($http) {
    var _getData2 = function(id_empresa) {
    return $http.post("php/index.php", id_empresa);
};

return {
    getData2: _getData2
}
}])

.controller("fluxoCtrl", function ($scope, $http, factCliente) {

    var id_empresa = {id_empresa: 1};
    factCliente.getData2(id_empresa).then(function(response) {
        $scope.mostraTodasContasEntradas = response;
    }, function(error) {
        console.log("Ocorreu um erro: " + error);
    });
});
</script>

PHP:

<?php
 require_once "../con/conexao.php";
 require_once "../classes/contaEntrada.php";

 $entrada = new contaEntrada();

 $payload = json_decode(file_get_contents('php://input'));
 $id_empresa = $payload->id_empresa;

 $entrada->mostraContasEntrada($id_empresa);
?>
  • I changed but now I’m getting this message on the console: "Undefined index: id_empresa in <b>/Applications/MAMP/htdocs/systems/system_web/fluxo_de_box/fluxojoin_v_hap/php/index.php</b> on line <b>15"

  • You can check in the console if the id_company variable is actually being postada

  • @Gustavosevero edited my answer, give a look now.

  • Alan, I’ve adjusted my code, as you suggested, and the message continues. "Undefined index: id_empresa in <b>/Applications/MAMP/htdocs/sistemas/sistemas_web/fluxo_de_caixa/fluxojoin_v_hap/php/index.php</b> on line <b>15</b><br /><b>Warning</b>: Cannot Modify header information - headers already sent by (output Started at /Applications/MAMP/htdocs/systems/systems_web/fluxo_de_box/fluxojoin_v_hap/php/index.php:15) in <b>/Applications/MAMP/htdocs/systems/systems_web/fluxo_de_box/fluxojoin_v_hap/classes/containEntrada.php</b> on line <b>52</b><br />"

  • Can you show the updated files?

  • I tried to remove the following line from the php: $id_company = $_POST['id_company']; And the message that appeared on the console was: "Syntaxerror:

  • Like.......????

  • Opa me empolguei no ingles, remove the $_POST from your index

  • The whole line, you mean?

  • Yes line , you removed ; from the end of line 2 of your js?

  • Yes I removed... My current code, is here in the post, updated... Which js line is to remove, exactly?

  • withdraw the ; at the end of the line angular.module("fluxo", ["ngRoute"]);

  • and remove the line $id_empresa = $_POST['id_empresa']; of your index.php

  • There, I set it... And it continues the same message.

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.