Error sending Ionic + PHP + Mysql form data

Asked

Viewed 466 times

0

I am trying to send data from my form, already all validated to my database in Mysql with PHP, but returns me the following error and does not send:

inserir a descrição da imagem aqui

What can I do wrong? I need this help.

My Controller:

    (function() {
    "use strict";

    angular.module("nhaac").controller("serVovoCtrl", function($scope, Data, $ionicScrollDelegate){



         // ENVIAR DADOS            
    $scope.cadastroVovo = function(vovo) {
            console.log(vovo);
            Data.setData(vovo).success(function(data) {               
                console.log(data.data);
                console.log("imprimiu console data");
                alert(data);            
            }).error(function(data) {
                alert(data? data: "Não foi possivel cadastrar");
            });

        console.log("Vai imprimir o vovos");    
        console.log(vovo);

    };

    });
})();

My Service:

    (function(){
    "use strict";

    angular.module("nhaac").value("Config", {

        getUrl: "http://vovocooks.com.br/admin/apis/put/"


    });

    angular.module("nhaac").service("Data", function($http, Config){


        //cadastro
        this.setData = function(dados){
         return $http({
                method: "POST",
                url: Config.getUrl + "rest-api.php",
                data: dados,
                headers : {
                                'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
                            }
            });
        };


    });
})();

My View (Form):

     <ion-view view-title="Tornar uma Vovó Cook" hide-nav-bar="false" >

            <ion-content delegate-handle="top" lazy-scroll >

                <h4>Cadastro de novos Cozinheiros</h4>   

            <form name="formCadastroVovo">

                <div class="list">

                  <label class="item item-input item-floating-label" >
                        <span class="input-label">Nome</span>
                        <input type="text" placeholder="Nome" name="nome" ng-model="vovo.nome" ng-required="true">
                        <span class="assertive" ng-show="formCadastroVovo.nome.$invalid && formCadastroVovo.nome.$dirty">Pereencha o campo nome corretamente!</span>
                  </label>

                  <label class="item item-input item-floating-label">
                        <span class="input-label">Email</span>
                        <input type="email" placeholder="Email"  name="email" ng-model="vovo.email" ng-required="true">
                        <span class="assertive" ng-show="formCadastroVovo.email.$invalid && formCadastroVovo.email.$dirty">Pereencha o campo email corretamente!</span>
                  </label>

                  <label class="item item-input item-floating-label">
                        <span class="input-label">CPF ou CNPJ</span>
                        <input type="number" placeholder="CPF ou CNPJ"  name="CPF" ng-model="vovo.CPF" ng-required="true">
                        <span class="assertive" ng-show="formCadastroVovo.CPF.$invalid && formCadastroVovo.CPF.$dirty">Pereencha o campo CPF ou CNPJ corretamente!</span>
                  </label>

                  <label class="item item-input item-floating-label" >
                        <span class="input-label">Identidade</span>
                        <input type="text" placeholder="Identidade" name="identidade" ng-model="vovo.identidade" ng-required="true">
                        <span class="assertive" ng-show="formCadastroVovo.identidade.$invalid && formCadastroVovo.identidade.$dirty">Pereencha o campo Identidade corretamente!</span>
                  </label>

                  <label class="item item-input item-floating-label">
                        <span class="input-label">CEP</span>
                        <input type="number" placeholder="CEP"  name="cep" ng-model="vovo.cep" ng-required="true">
                        <span class="assertive" ng-show="formCadastroVovo.cep.$invalid && formCadastroVovo.cep.$dirty">Pereencha o campo CEP corretamente!</span>
                  </label>

                  <label class="item item-input item-floating-label" >
                        <span class="input-label">Logradouro</span>
                        <input type="text" placeholder="Endereço" name="logradourro" ng-model="vovo.logradourro" ng-required="true">
                        <span class="assertive" ng-show="formCadastroVovo.logradourro.$invalid && formCadastroVovo.logradourro.$dirty">Pereencha o campo Endereço corretamente!</span>
                  </label>

                  <label class="item item-input item-floating-label">
                        <span class="input-label">Número</span>
                        <input type="text" placeholder="Número"  name="numero" ng-model="vovo.numero" ng-required="true">
                        <span class="assertive" ng-show="formCadastroVovo.numero.$invalid && formCadastroVovo.numero.$dirty">Pereencha o campo Número corretamente!</span>
                  </label>

                   <label class="item item-input item-floating-label" >
                        <span class="input-label">Complemento</span>
                        <input type="text" placeholder="complemento" name="complemento" ng-model="vovo.complemento">                    
                  </label>

                   <label class="item item-input item-floating-label" >
                        <span class="input-label">Bairro</span>
                        <input type="text" placeholder="Bairro" name="bairro" ng-model="vovo.bairro" ng-required="true">
                        <span class="assertive" ng-show="formCadastroVovo.bairro.$invalid && formCadastroVovo.bairro.$dirty">Pereencha o campo Bairro corretamente!</span>
                  </label>

                  <label class="item item-input item-floating-label" >
                        <span class="input-label">Cidade</span>
                        <input type="text" placeholder="Cidade" name="cidade" ng-model="vovo.cidade" ng-required="true">
                        <span class="assertive" ng-show="formCadastroVovo.cidade.$invalid && formCadastroVovo.cidade.$dirty">Pereencha o campo Cidade corretamente!</span>
                  </label>

                  <label class="item item-input item-floating-label" >
                        <span class="input-label">Estado</span>
                        <input type="text" placeholder="Estado" name="estado" ng-model="vovo.estado" ng-required="true">
                        <span class="assertive" ng-show="formCadastroVovo.estado.$invalid && formCadastroVovo.estado.$dirty">Pereencha o campo Estado corretamente!</span>
                  </label>


                  <label class="item item-input item-floating-label" >
                        <span class="input-label">Responsável por Contato</span>
                        <input type="text" placeholder="Responsável por Contato" name="responsavel_contato" ng-model="vovo.responsavel_contato" ng-required="true">
                        <span class="assertive" ng-show="formCadastroVovo.responsavel_contato.$invalid && formCadastroVovo.responsavel_contato.$dirty">Pereencha o campo Responsável corretamente!</span>
                  </label>

                  <label class="item item-input item-floating-label">
                        <span class="input-label">Telefone Fixo</span>
                        <input type="number" placeholder="Telefone Fixo" name="telefone_empresa" ng-model="vovo.telefone_empresa">
                  </label>

                   <label class="item item-input item-floating-label">
                        <span class="input-label">Celular de Contato</span>
                        <input type="number" placeholder="Celular de Contato" name="celular" ng-model="vovo.celular" ng-required="true">
                        <span class="assertive" ng-show="formCadastroVovo.celular.$invalid && formCadastroVovo.celular.$dirty">Preencha o campo Celular corretamente!</span>
                  </label>


                  <label class="item item-input item-floating-label">
                        <span class="input-label">Senha</span>
                        <input type="password" placeholder="Senha"  name="senha" ng-model="vovo.senha" ng-required="true">
                        <span class="assertive" ng-show="formCadastroVovo.senha.$invalid && formCadastroVovo.senha.$dirty">Pereencha o campo senha corretamente!</span>
                  </label>

                </div>
            </form>

            <button class="button button-full button-positive" ng-disabled="formCadastroVovo.$invalid" ng-click="cadastroVovo(vovo)">Enviar</button>

            </ion-content>

    </ion-view>

My PHP:

<?php

header("Access-Control-Allow-Origin:*");
header("Content-Type: application/html; charset=UTF-8");

    //formulário
    $data = file_get_contents("php://input");
    $objData = json_decode($data);

    // TRANSFORMA OS DADOS

    $CPF = $objData->CPF;
    $bairro = $objData->bairro;
    $celular = $objData->celular;
    $cep = $objData->cep;
    $cidade = $objData->cidade;
    $email = $objData->email;
    $estado = $objData->estado;
    $identidade = $objData->identidade;
    $logradourro = $objData->logradourro;
    $nome = $objData->nome;
    $numero = $objData->numero;
    $responsavel_contato = $objData->responsavel_contato;
    $senha = $objData->senha;
    $telefone_empresa = $objData->telefone_empresa;


    // INSERE OS DADOS
    $db = new PDO("mysql:host=ohostaqui;dbname=vovocooks_db", "vovocooks_db", "rain14570");

    if($db){
        $sql = "INSERT INTO fornecedores VALUES (CPF, bairro, celular, cep, cidade, email, estado, identidade, logradourro, nome, numero, responsavel_contato, senha, telefone_empresa) VALUES ('".$CPF."', '".$bairro."', '".$celular."', '".$cep."', '".$cidade."', '".$email."', '".$estado."', '".$identidade."','".$logradourro."', '".$nome."', '".$numero."', '".$responsavel_contato."', '".$senha."','".$telefone_empresa."')";

        $query = $db->prepare($sql);

        $query ->execute();

        echo json_encode(array('message'=> ' Os dados foram inseridos com sucesso. Obrigado e bem vindo!' ));
    }else{
        echo json_decode(array('message'=> ' Não foi possivel iserir os dados! Tente novamente mais tarde!' ));
    };

?>

The structure of the table:

inserir a descrição da imagem aqui

  • Where is the PHP code?

  • I thought I had posted, is there... Thank you.

  • I took advantage and put the table structure.

  • The undefined seems to me to be exactly why the sent object is empty (or the attribute .data). Also, it seems that you keep the password directly in the bank without encryption (I suggest an MD5()) there on INSERT.

  • Hi @leonardopessoa yes, the date is empty, but I throw the complete objects. I am not able to solve.

  • So it does not give error? It only appears as executed but the value is not entered in the bank. That’s it?

  • That’s right. @leonardopessoa

  • Was any of the answer helpful? Don’t forget to choose one and mark it so it can be used if someone has a similar question!

Show 3 more comments

1 answer

1


You have to create not only the vovo model, but all its elements and when send in the request, + or - like this:

$Scope.vovo = { 'name' means '', etc etc etc... }

Also it is to use Data.setData($Scope.vovo), this should solve...

  • I came back just to thank you. Thank you so much for your help! :)))

Browser other questions tagged

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