php cadastre with angular js

Asked

Viewed 449 times

0

Hello!

I have the following controller at the angle:

myApp.controller('cadastrarClienteController', function($scope, $http){
$scope.cadastrarCliente = function () {
$cliente = {
    CliNome: $scope.cli_nome,
    CliTelefone: $scope.cli_telefone,
    CliEmail: $scope.cli_email,
    CliDescricao: $scope.cli_descricao
};

var response = $http({
    method: "post",
    url: baseUrl + "/index.php/Clientes/create",
    data: JSON.stringify($cliente),
    dataType: "json"
});
return response;});

It’s right the way I’m working?

create no php

public function create() {

    if ($this->method == 'POST') {

        $var = json_decode($_POST['cliente']);
        var_dump($var);

        /*$nome = $this->input->post('cli_nome');
        $descricao = $this->input->post('cli_descricao');
        $telefone = $this->input->post('cli_telefone');
        $email = $this->input->post('cli_email');

        $data = array(
            'cli_nome'      => $nome,
            'cli_descricao' => $descricao,
            'cli_telefone'  => $telefone,
            'cli_email'     => $email,
        );
        $res = $this->clientes_model->create_cliente($data);*/
    }
}

I don’t know what exactly to do inside this if. That is, how to "take" the data that comes by POST sent by angular.

is in the response that returns that worked?

Thank you

  • If you are coming by POST, have you ever tried to pick up using the $_POST['<variavel>'] ? What would go to php would be a Json, so you can try to catch it using json_decode.

  • make a json_decode and then a $_POST?

  • In this case, try the following: $var = json_decode($_POST['client']); Then var_dump $var to see what appears.

  • I tried but it didn’t work out.. I changed the question to show how I’m doing it

2 answers

0

public function create($cli_nome, $cli_telefone, $cli_email, $cli_descricao) {
  /* Restante do código */
}

$cli_nome = $_POST["cli_nome"];
$cli_telefone = $_POST["cli_telefone"];
$cli_email = $_POST["cli_email"];
$cli_descricao = $_POST["cli_descricao"];

create($cli_nome, $cli_telefone, $cli_email, $cli_descricao);

Try to make the request and then send it to the function as parameter.

0

Good afternoon. Then I work with the API format and the front in Angular. First you have to download and integrate into the code a lib of this link.

Read it well, it’s simple, really. After that, there at the angle, you will set the url to send the request to the code.

In the code code, you will receive the information like this:

public function Formulario_post(){

    $nomeCliente = $this->post('cli_nome');
    //outras variáveis
    //segue para as validações e models

}

After processing the information and persisting or not in the database, you should return an HTTP code saying whether it worked or not. I advise you to read this link also to be able to understand.

I hope I’ve helped.

Oss!

Browser other questions tagged

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