Textarea Characters break line when submitting to database

Asked

Viewed 53 times

1

I have a form with a field SELECT and by submitting the text in the database, it breaks the line, even copied and pasted directly from the table record in the notepad and is even broken, in the HTML the SELECT is like that:

<textarea id="descricao" name="descricao" wrap="physical" class="form-control input-lg" placeholder="Insira a descrição relacionada..." rows="22" draggable="true"></select>

Before being entered into the database, the value of TEXTAREA undergoes a validation:

<?php
if( !function_exists('filtra_var') ){
    function filtra_var($var) {
        $var = trim($var);
        $var = strip_tags($var);
        $var = addslashes($var);
        return $var;
    }
}

$descricao = $_POST['desricao']
$desricao = filtra_var( $descricao );

/* INSERT IN DB */
?>

The text below I insert it like this, without breaking:

What is Lorem Ipsum?

Lorem Ipsum is Simply dummy text of the Printing and typesetting Industry. Lorem Ipsum has been the Industry’s standard dummy text Ever Since the 1500s, when an Unknown Printer Took a galley of type and scrambled it to make a type specimen book.

Retrieving table record with PHP:

<?php
$descricao = stripslashes( $row['descricao'] ); // $row == mysqli_fetch_array( $query_sql, MYSQLI_ASSOC); 

$descricao = nl2br ( $descricao );

echo $descricao;
?>

The text returned on the screen gets broken:

What is Lorem Ipsum?

Lorem Ipsum is Simply dummy text of the Printing

and typesetting Industry. Lorem Ipsum has been the

Industry’s standard dummy text Ever Since the 1500s,

when an Unknown Printer Took a galley of type

and scrambled it to make a type specimen book.

Well, there is no line break, I mean, <br>, besides "What is Lorem Ipsum?" and the text is broken, it should be in the database like this:

What is Lorem Ipsum?

Lorem Ipsum is Simply dummy text of the Printing and typesetting Industry. Lorem Ipsum has been the Industry’s standard dummy text Ever Since the 1500s, when an Unknown Printer Took a galley of type and scrambled it to make a type specimen book.

  • The function nl2br adds a <br> before all line breaks, that is, your string contains line breaks yes. It is important to remember that HTML considers a line break only as a whitespace, in theory a space and a \n are rendered in the same way (depending on the tag where it is rendered, see Docs of the CSS property whitespace)

  • But in the database already enters broken. I know the importance, so I used, but the behavior is abnormal.

  • If in the database already enters broken is because the textarea received line break. Your own example (the yellow part) has line breaks.

  • No, sir. I assure you I didn’t, I pasted from the notebook and checked before, I did N tests.

  • So I don’t have enough information to help. Try to glue in that fiddle and see if he accuses any line breaks.

1 answer

0

I solved the problem, I incremented the code that validates the value before being inserted into the database.

<?php
if( !function_exists('filtra_var') ){
    function filtra_var($var) {
        $var = trim($var);
        $var = strip_tags($var);
        $var = addslashes($var);
        return $var;
    }
}

$descricao = $_POST['desricao']
$desricao = filtra_var( $descricao );
$descricao = nl2br( $descricao, false );

/* INSERT IN DB */
?>

So the text is sent with the line break and instead of \n shall be inserted in the <br>.

And in the impression of value it was like this:

<?php
$descricao = stripslashes( $row['descricao'] ); // $row == mysqli_fetch_array( $query_sql, MYSQLI_ASSOC); 

$descricao = stripslashes( $descricao );

echo $descricao;
?>

Browser other questions tagged

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