Localstorage doesn’t keep names with accents?

Asked

Viewed 307 times

0

My Ionic app stores user data, as a name, in localStorage. I went for a test, logged in my wife’s name, Flávia, but when I looked at the console in the Resources tab, the name field was null and the id had her user id. I went to the bank and changed her name, I removed the accent from the letter a. I went back, I logged and her name appeared! Do you know if localStorage allows you to store accented names? If not, how should I do?

Part of index.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>

My login.html:

<ion-view title="Login" hide-back-button="true">
<ion-content overflow-scroll="true" padding="true" class="has-header">
    <form class="list">
        <ion-list>
            <div ng-controller="loginCtrl">
                <label class="item item-input">
                    <input type="text" ng-model="usuario.email" placeholder="E-mail">
                </label>
                <label class="item item-input">
                    <input type="password" ng-model="usuario.senha" placeholder="Senha">
                </label>
            </ion-list>
            <div class="spacer" style="height: 40px;"></div>
            <button class="button button-stable button-block" ng-click="logar(usuario)">Entrar</button>
            <!--<a href="#/salas" class="button button-stable button-block ">Entrar</a> -->
            <a href="#/cadastroCep" class="button button-stable button-block ">Cadastre-se</a>
            <div align="center">{{msgErro}}</div>
            </div>
    </form>
</ion-content>

My controller.js

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

$scope.usuario = {
    email: "",
    senha: ""
}

$scope.msgErro = '';

$scope.msgExiste = '';

$scope.logar = function (usuario) {

    $http.post("http://www.vigilantescomunitarios.com/www/php/login.php", usuario).success(function (response){

        if(response == ''){
            $location.path('/page10');
            $scope.msgErro = "E-mail ou senha inválido";

        }else if(typeof(Storage) !== "undefined") {
                    $window.localStorage.setItem("idUsuario", response.idUsuario);
                    $window.localStorage.setItem("idCep", response.idCep);
                    $window.localStorage.setItem("nome", response.nome);
                    $location.path('/salas');
                } else {
                    console.log("Desculpe, mas o navegador nao possui suporte a Web Storage.");
                }
      })
   }
})

Console print: console

  • login.html is saved as utf8-without-good by your text editor (Notepad++ or sublimetext for example)?

  • 1

    After that line, }else if(typeof(Storage) !== "undefined") { , add that console.log("Nome: "+response.nome); . Test using an accentuated name and see what appears on the browser console. On Chrome, press Ctr+Shift+I

  • It’s @Danielomine, it looked like this on the console: Name: null .

  • I suspected from the beginning.. It can be the script on the page ww/php/login.php that is returning wrong. Check what is wrong with this script because apparently there is no error in the javascript that presented the question.

  • Exactly @Danielomine, I put a print_r on at the end of login.php to see how the data is coming and just see how the name is: Array ( [idUsuario] => 45 [idCep] => 66 [userUsuario] => C [name] => Fl via Schneider [email] => [email protected] ) How can I fix this in php?

  • configure the php environment’s Find. There are several topics here on the subject. It’s a rather complicated and extensive subject to explain in the comments. Check which database charset and configure php according to the database charset. Basically.

  • No, the database charset is utf8, but from what I saw I have to do handling the name variable in php itself

  • so blz, I can’t say and say "do X, do Y", but I think you can just add the header in the php file. header('Content-Type: text/html; charset=utf-8');. Also check that the text editor you used to edit the PHP file is as utf-8 without bom. Also check what returns the function echo mb_internal_encoding(); exit;. But they are only guesses. As I said, there is no way to solve without knowing how the whole environment really is. Note that even the database configured as utf8, the data may be being recorded erroneously with another charset, ie corrupted.

Show 3 more comments

2 answers

1

This is because the file www/php/login.php is coming without accents

I always tell you to post the code that can be reproduced, but it seems that you always fail in this, again please to avoid difficulties in helping you follow the tips of this link: /help/mcve

I don’t know how this your PHP, but for the accents to work you have to do as in this answer:

  • Doubt with charset=iso-8859-1 and utf8

    • Save all PHP and HTML documents with the same encoding utf8 without GOOD or ANSI (do not use ANSI if your bank is utf8)
    • If it is utf8 you should open the connection like this (in your login.php) if using PDO:

      $conn = new PDO('mysql:host=HOST;dbname=BANCO;charset=utf-8', 'USUARIO', 'SENHA');
      $conn->exec('SET CHARACTER SET utf8');//Define o charset como UTF-8
      

      or so if using mysqli:

      $mysqli = new mysqli('HOST', 'usuario', 'senha', 'banco');
      
      if (false === $mysqli->set_charset('utf8')) {
          printf('Error ao usar utf8: %s', $mysqli->error);
      }
      

Do not use the old mysql API for PHP (those that start with mysql_):

-1

It seems to be the encoding of your file. In the <head> of your index.html add this line:

<meta charset="utf-8"/>
  • No, I got this meta charset in the code.

  • @Gustavosevero you could put the code snippet where the error occurs in Codepen?

  • 1

    I just posted the codes here. OBS: I don’t know how to work the codeopen kkkkk

  • 1

    Only a note @SCOFIELD was not me who negatively, I’ve been avoiding doing it. Really the question is very vague, when it is so it is preferable to close the question until it presents the minimum data to be able to answer, I could only understand the problem because of a comment there in the question.

Browser other questions tagged

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