How to pick up object in php

Asked

Viewed 422 times

0

Good evening, everyone,

I’m trying to pass parameters from my controller to php that searches the database and I’m not getting it. I’m passing the data as json object.

controller:

var buscaCategorias = function(){

    var idempresa = $window.localStorage.getItem('idemp');
    var opcao = 'pegarCategoria';
    var buscaCat = {
        "opcao": opcao, 
        "idempresa": idempresa
    };

    console.log(buscaCat);

    $http.post('http://localhost:8888/sistemas/webApps/fluxo_de_caixa/fluxojoin_2.0/php/buscaCatSubcat.php', buscaCat).success(function(data){
        console.log(data);
    });
};

buscaCategorias();

In this console.log(buscaCat), what it shows is Object {option: "pegarCategoria", idempresa: "3"}

PHP:

<?php
ini_set('display_errors', true);
error_reporting(E_ALL);

include_once("con.php");

$pdo = conectar();

$data = file_get_contents("php://input");
$data = json_decode($data);

$opcao = $data->opcao;

switch ($opcao) {
    case 'pegarCategoria':

        $idempresa = $data->idempresa;

        $buscaCategoria=$pdo->prepare("SELECT categoria, idcategoria FROM categoria WHERE empresa_idempresa=:idempresa ");
        $buscaCategoria->bindValue("idempresa", $idempresa);
        $buscaCategoria->execute();

        $return = array();

        while ($linhaCat=$buscaCategoria->fetch(PDO::FETCH_ASSOC)) {

            $linhaCat['categoria'] = $linhaCat['categoria'];
            $linhaCat['idcategoria'] = $linhaCat['idcategoria'];
            $return = $linhaCat;

        }

        echo json_encode($return);

        break;
  • Trying to pass an object on $http.get, that works?

  • I don’t know kkkkk

  • I can only do this using $routeParams?

  • Don’t need quotes I know inside the object keys searchCat. var buscaCat = {&#xA; opcao : opcao, &#xA; idempresa: idempresa&#xA; };

  • I took this example from w3schools kkkkk’s website

  • I’m already accessing the bank. But now only 1 dice, of 2, is coming.

Show 1 more comment

1 answer

3


You got some problems there.

First, the verb GET has no body, data must be passed to the URL via querystring.

So much so that if you open the documentation of $http.get will realize that the second parameter is an object of configuration and not the data you want to pass to the server side. The documentation says:

config | Object | Optional Configuration Object

Only knowing this you will already need to change some things (in the application and also rethink what you are doing).

But there’s also another little problem on the PHP side.

file_get_contents("php://input"); serves to obtain the data received by POST and not by GET.


Possibly only change the GET request to POST will show results.

$http.post('http://urlEnormeAqui/buscaCatSubcat.php', buscaCat).success(function(){ });
  • You suggest I switch from . get to . post, then?

  • 1

    I can’t suggest anything without understanding what you need, but said it will possibly bring the expected result.

  • I need to search two bank data. I managed to bring, but only one of them is coming.

Browser other questions tagged

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