htmlentities () does not work

0

Hello,

I'm saving HTML code to a session variable:

$_SESSION["recuperarInvalido"] = "<p id='recuperarInvalido'> O e-mail introduzido é inválido! </p>";

But when typing the value of the session variable:

echo htmlentities($_SESSION["recuperarInvalido"]);

In the browser it looks like this:

<p id='recuperarInvalido'> O e-mail introduzido é inválido! </p>

But it should look like this:

O e-mail introduzido é inválido!
    
asked by anonymous 16.03.2016 / 14:18

3 answers

2
echo $_SESSION["recuperarInvalido"];

Just remove the "htmlentities ()" because it was done to convert the html into string, to literally show the html on the front!

    
16.03.2016 / 14:25
2

Miguel,

I looked it up here, and according to w3schools the htmlentities function is used to just do the opposite of what you want. It was made so that a String is completely displayed by the browser, no matter what its content. So char < and > are converted to &lt; and &gt; and are not interpreted by the browser as html tags, but as text.

To get what you want, simply remove the htmlentities function:

echo $_SESSION["recuperarInvalido"];

However, I must warn you that this will work, but it is not the best approach. I think it's not good practice to write HTML code in the backend, I usually leave the HTML for the template file.

The user Guilherme posted a answer just below that leaves the php code of your backend, more separate from your HTML code.

    
16.03.2016 / 14:35
1

Let's see, you could store session values in a variable $session

$session = $_SESSION["recuperarInvalido"] = 'O e-mail introduzido é inválido!';

Here you check if there is $_SESSION['recuperarInvalido'] , if there is yes you have the browser render.

<?php
        if (isset($session)) {
        ?>
            <p id='recuperarInvalido'> ><?php echo $session; ?> </p> 
    <?php } ?>

In this way I find it more practical.

    
16.03.2016 / 14:31