Raúl Vázquez Raúl Vázquez - 6 months ago 12
PHP Question

PHP/HTML - Echo an error in html div that has another echo in php?

thanks for your help in advance, noob here.

So, i have this error div of a form.

<div><?php echo $error; ?></div>


I'm trying to validate an email with php, but i want its error to change depending of the language selected by the user. I have another two php files with arrays with the translated text. What i'm tryng to do is to echo the error that contains the echo for the array variable so it can be interpreted as multilingual text in the html. I know that you can't echo an echo, but i need a workaround for this. Here is the php part.

<?php

$error = '';

if ($_POST) {

if (filter_var($_POST['mail'], FILTER_VALIDATE_EMAIL) === false) {

$error .= '<?php echo $lang["EMAIL_INVÁLIDO"]; ?>';

};

if ($error != "") {

$error = '<p>' . $error . '</p>';

};

};

?>


Thanks!

Answer

instead of direct echo, try assigning it to the variable and then echo the variable.

<?php
$error = '';
if ($_POST) {
    if (filter_var($_POST['mail'], FILTER_VALIDATE_EMAIL) === false) {
        $error .= $lang["EMAIL_INVÁLIDO"]; 
    };
    if ($error != "") {
        $error = '<p>' . $error . '</p>';
    };
};
?>
Comments