UTF-8 problems in PHP project [duplicate]

1

I'm learning PHP and am experiencing some problems with special characters, see:

Thefunnythingisthatinthetableitemsitdisplaysaccentandspecialcharacters.Andyes,I'vetriedusingthe<metacharset="UTF-8"/>

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

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

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

Still the problem 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 table form:

<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 there!

    
asked by anonymous 03.06.2016 / 15:59

2 answers

3

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

  • Set the meta tag in HTML for utf-8
  • Set charset for utf-8 in PHP (via header or set)
  • All your .html, .php, and .php files should use UTF-8 encoding without good .
  • Define 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 connection charset:

mysqli_set_charset($conexao,"utf8");
  

Change Encoding of Files (Notepad ++):

And help yourself, look for an IDE rather than a notepad mejorzin .... will facilitate learning / work.

    
03.06.2016 / 17:51
0

Before writing any HTML output, try the command below:

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

It's also important to check the charset of the publisher (or publishers) you're using.

I hope I have helped!

    
03.06.2016 / 17:35