What's wrong with my array in php?

1

I'm trying to mount an array, in php, that returns to my controller, because the render is displaying on the 'undefined' console, why?

Controller

.controller('usuarioCtrl', function ($scope, $http, $window, $location) {

$scope.salvaUsuario = function (usuario) {
    var idCep = $window.localStorage.getItem('idCep');
    usuario.idCep = idCep;

       $http.post("http://localhost:8888/sistemas/webApps/ionic/vcApp/www/php/salvaUsuario.php", usuario).success(function (data){

        console.log(data);
        var ema = $window.localStorage.setItem("emailLogin", data.email);
        //console.log(JSON.stringify(data));
        if(data.cod === 1){
            $location.path('/cadastraUsuario');
            $scope.msgExiste = "Usuário já existente. Tente outro.";
        }

        });
        $location.path('/page10');
    }
})

PHP

<?php
header('Content-Type: text/html; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type,X-Prototype-Version,X- Requested-With');

mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');

include_once("conPDO.php");

$pdo = conectar();

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

$nome = $data->nome;
$usuario = $data->usuario;
$email = $data->email;
$senha = $data->senha;
$idCep = $data->idCep;

$nome = utf8_decode($nome);
$tipoUsuario = "C";

$verificaUsuario=$pdo->prepare("SELECT * FROM usuarios WHERE nome=:nome AND email=:email");
$verificaUsuario->bindValue("nome", $nome); 
$verificaUsuario->bindValue("email", $email); 
$verificaUsuario->execute();

$quant = $verificaUsuario->rowCount();

if($quant != 1){

    $result = array([
       'email' => $email
    ]);

    $insereUsuario=$pdo->prepare("INSERT INTO usuarios (idUsuario, idCep, tipoUsuario, nome, usuario, email, senha) VALUES (?, ?, ?, ?, ?, ?, ?)");
    $insereUsuario->bindValue(1, NULL); 
    $insereUsuario->bindValue(2, $idCep); 
    $insereUsuario->bindValue(3, $tipoUsuario); 
    $insereUsuario->bindValue(4, $nome);
    $insereUsuario->bindValue(5, $usuario);
    $insereUsuario->bindValue(6, $email);
    $insereUsuario->bindValue(7, $senha);

    $insereUsuario->execute();

    echo json_encode($result[0]);

}else{

    $result = array(
       'cod' => 1,
      );

    echo json_encode($result);

    return false;

}
    
asked by anonymous 14.04.2016 / 22:39

2 answers

1

Friend I tested here on my site.

I came to the conclusion that you should use ['CAMPO'] .

So it would be:

json_encode($result['email'])

Unfortunately, I can not tell you why it should be used like this, but I'll give it a kick, here it goes: As you can see, your array has values set as if it were a Json (Comparison Example) should pull as if it were with a Json, but instead of using -> use [''] .

I hope you can tidy up your system, take a look at my website that has the link below, there I'll show you the steps I used to come to that conclusion.

Good luck with your project, anything I'm here to help; D

As I show here on my site: link

    
15.04.2016 / 05:39
1

Some problems I see in your code:

1 - If it is a json that you are returning in the response, the Header should be compatible. Ex: Instead of header ('Content-Type: text / html; charset = utf-8'); use header ('Content-Type: application / json ; charset = utf-8');

2 - No need to use:

$result = array([
   'email' => $email
]);

Then you can use echo json_encode($result[0]); You can do direct echo json_encode(array('email' => $email));

3 - And in the else when you return a "cod = 1", but in the angular you try to access the data.email before comparing the cod, so if you fall into that else it will give an undefined error because the data.email does not exist just the data.cod . Or if you still want to retune the email too, return echo echo json_encode(array('cod' => 1 , 'email' => $email));

4 - That return false at the end is already there: p

    
15.04.2016 / 06:17