Problems with UTF-8 in PHP project

Asked

Viewed 1,272 times

1

I am learning PHP and I am facing some problems with the special characters, see:

Veja circulado em vermelho

the funny thing is that in the table items it displays the accent and the special characters. And yes, I’ve tried using the settings <meta charset="UTF-8"/>

and also <?php ini_set('default_charset','UTF-8');?>

until the <meta http-equiv="content-Type" content="text/html; charset=iso-8859-1" />

and <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

the problem still persists.

This is my main form:

<html>
    <head>
        <?phpini_set('default_charset','UTF-8');?>
        <title>Gerenciação de Tarefas</title>
    </head>

    <body>

        <h2>Gerenciador de Tarefas</h2>

        <?php include('formulario.php'); ?>
        <?php if ($exibir_tabela) : ?>
            <?php include('tabela.php'); ?>
        <?php endif; ?>

    </body>

</html>

This is the registration form:

<meta charset="UTF-8"/>
<form>
    <fieldset>
        <legend><b>Nova tarefa</b></legend>
        <?php 
            $_id = $tarefa["id"];
            $_nome = $tarefa["nome"];
            $_descricao = $tarefa["descricao"];
            $_prioridade = $tarefa["prioridade"];
            $_prazo = $tarefa["prazo"];
            $_concluida = $tarefa["concluida"];
        ?>

        <input type = "hidden" name = "id"
            value = "<?php echo $_id; ?>" />

        <label>
            Tarefa
            <br>
            <input type = "text" name = "nome" 
                value = "<?php echo $_nome; ?>"/>
        </label>
        <br><br>

        <label>
            Descrição(Opcional)
            <br>
            <textarea name = "descricao"><?php echo $_descricao; ?></textarea> 
        </label>
        <br><br>

        <label>
            Prazo(Opcional)
            <br>
            <input type = "text" name = "prazo"
                value = "<?php echo traduz_data_para_view($_prazo); ?>"/>
        </label>
        <br><br>

        <fieldset>
            <legend>Prioridade</legend>
            <label>
                <input type = "radio" name = "prioridade" value = "1"<?php echo ($_prioridade == 1)?'checked':''; ?>/>
                Baixa

                <input type = "radio" name = "prioridade" value = "2" <?php echo ($_prioridade == 2)?'checked':''; ?>/>
                Média

                <input type = "radio" name = "prioridade" value = "3"<?php echo ($_prioridade == 3)?'checked':''; ?>/>
                Alta
            </label>
        </fieldset>
        <br><br>

        <label>
            <input type = "checkbox" name = "concluida" value = "1" <?php echo ($_concluida == 1)?'checked':''; ?>/>
            Tarefa Concluída
        </label>
        <br><br>

        <input type = "submit" value = "<?php echo ($tarefa["id"] > 0) ? "Atualizar" : "Cadastrar"; ?>"/> 
    </fieldset>

</form>

This is the form of the table:

<meta charset="UTF-8"/>
<form>
    <fieldset>
        <table border = 1>
            <tr>
                <th>Tarefas</th>
                <th>Descrição</th>  
                <th>Prazo</th>
                <th>Prioridade</th>
                <th>Concluída</th>
                <th>Opções</th>
            </tr>

            <?php if($lista_tarefas != null){
            foreach ($lista_tarefas as $tarefa) : ?>
                <tr>
                    <td><?php echo $tarefa["nome"]; ?></td>
                    <td><?php echo $tarefa["descricao"]; ?></td>
                    <td><?php echo traduz_data_para_view($tarefa["prazo"]); ?></td>
                    <td><?php echo traduz_prioridade($tarefa["prioridade"]); ?></td>
                    <td><?php echo traduz_concluida($tarefa["concluida"]); ?></td>
                    <td> 
                        <a href="editar.php?id=<?php echo $tarefa["id"];?>">
                        Editar
                        </a>
                    </td>
                </tr>
            <?php endforeach; 
            };?>

        </table>
    </fieldset>
</form>

This started when I separated the form for better maintenance, the 3 forms are up there!

2 answers

3


To avoid problems with special characters you need to ensure that all aspects of your project use the same charset.

  • Set the meta tag in HTML to utf-8
  • Set the charset to utf-8 in PHP (via header or set)
  • All your . html, . php files should use encoding UTF-8 without good (utf-8 without GOOD).
  • Set your connection/data transmission between application and DB for the same charset.

From the answers and comments above, the first 2 you’ve done, do the rest that will work.

Change the connection charset:

mysqli_set_charset($conexao,"utf8");

Change File Encoding (Notepad++):

Alterar encoding notepad

And help yourself, look for an IDE, not a better notepad.... will make learning/working easier.

  • That’s right, thanks, I looked for that answer! I’m going to use Dreamweaver from now on!

  • @Juliocesardasilvabarros practically the same thing from here http://answall.com/questions/43193/d%C3%bavida-com-charset-iso-8859-1-e-utf8

0

Before writing any output in HTML, try the command below:

<?php 
header('Content-Type: text/html; charset=utf-8');

It is also important to check the charset of the editor (or editors) you are using.

I hope I’ve helped!

Browser other questions tagged

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