echo is printing HTML tags

6

I made a script for newsletter subscription, the user sends the email to cadastre.php and from there the script receives an echo in a message with a button, the problem is that the button does not appear, what appears is the tag, see :

Script:

$(function()
{
    $("#ok").click(function()
    {
        $("#formulario_news").hide("slow");
        BeforeSend:$("#carregando_news").show("slow");
        var email = $("#email").val();
        $.post("<?php bloginfo('template_directory');?>/newsletter/cadastro.php",{email: email}, function(data)
        {
            complete:$("#carregando_news").hide("slow");
            $("#retorno").show("slow").text(data);
            $("#voltar").click(function()
            {
                $("#retorno").hide("slow");
                $("#formulario_news").show("slow");
            });
        });
    });
});
<div id="newsletter">

        <h1>/Assine nosso newsletter</h1>

        <div id="formulario_news">
            <span>Informe seu email:</span>
            <input type="text" id="email" name="email"/>
            <input type="submit" name="ok" id="ok" class="btn_ok" value="OK"/>
        </div>

        <div id="carregando_news" style="display:none;">
            <img src="<?php bloginfo('template_directory'); ?>/imagens/ajax-loader.gif"/> <p> Aguarde, enviando...</p>
        </div> <!-- Carregando newsletter -->

        <div id="retorno" style="padding:10px;border:1px solid #0F0;background:#C1FFD1;width:170px;margin-left:3px;margin-bottom:3px;display:none;font:14px Trebuchet;
            font-weight:bold;">
        </div>

</div>

cadastro.php:

$email = strip_tags(trim($_POST['email']));
if(empty($email))
{
    echo "Informe seu email</br>";
    echo "<button type="button" id="voltar"/>Voltar</button>";
}
elseif(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
    echo "Informe um email válido</br>";
    echo "<button type="button" id="voltar"/>Voltar</button>";
}

Let's assume that the user does not enter the email and click the OK button. The return is this:

Informe seu email</br>
<button type="button" id="voltar"/>Voltar</button>

The tags appear written, the browser does not jump line and the button does not appear.

    
asked by anonymous 28.12.2018 / 13:41

2 answers

5

The problem is here:

$("#retorno").show("slow").text(data);
                            ↑

When using the .text() method, tags are treated as text. Use the .html() method:

$("#retorno").show("slow").html(data);
                            ↑

The .html() method will insert the value of data as HTML instead of text.

  

As I mentioned, there is a break in the string of echo :

echo "<button type="button" id="voltar"/>Voltar</button>";

You are delimiting the string with double quotation marks and using the same quotation marks within the string. Swap the double quotation marks within the string with single quotation marks:

echo "<button type='button' id='voltar'>Voltar</button>";
                   ↑      ↑    ↑      ↑

The button tag does not require a closing of the form you put in:

echo "<button type='button' id='voltar'/>Voltar</button>";
                                       ↑
                                    errado!

The tag is closed in </button> .

    
28.12.2018 / 13:50
6
The problem is in your echo you are using double quotation marks to declare the content of echo and as in your tag html there are double quotation marks you must use single quotation marks as in the example below or concatenate with a backslash where you need to use the double quotes.

$email = strip_tags(trim($_POST['email']));
if(empty($email))
{
    echo 'Informe seu email</br>';
    echo '<button type="button" id="voltar"/>Voltar</button>';
}
elseif(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
    echo 'Informe um email válido</br>';
    echo '<button type="button" id="voltar"/>Voltar</button>';
}
    
28.12.2018 / 13:56