简体   繁体   中英

How to show carriage return from textarea input?

In my phpmyadmin, I can see that the linebreaks or carriage returns are recognized in the database entries however when they are displayed on the website, the carriage returns are ignored and everything is joined together.

Right now I have the text aligned to the left. I don't know if that is the reason for this.

I tried to get the keystrokes then if 13 or carriage return was entered, store a <br> instead or something... I even tried manually enterring &#10; and /r/n but those are also ignored, they are stored in the db.

So what can I do?

Here is what I tried briefly regarding the replace

function build_post() {
    this.value = "";
}

var message = new build_post();

function addtomessage(fnc, keynum) {
    fnc.value = message.value + keynum;
}

function myKeyPress(e) {
    var keynum;

    if (window.event) { // IE
        keynum = e.keyCode;
    } else if (e.which) { // Netscape/Firefox/Opera
        keynum = e.which;
    }

    count_char();

    if (keynum == 13) {
        linebreak = '<br>';
        addtomessage(message, String.fromCharCode(linebreak));
    } else {
        addtomessage(message, String.fromCharCode(keynum));
    }
}

Then the result is echoed in between the textarea tags

The solution may be inside your php code. Try to output text from db via nl2br function (php).

nl2br — Inserts HTML line breaks before all newlines in a string

for example:

<?php

    $q = mysql_query("< your query >");
    $f = mysql_fetch_assoc($q);

    echo nl2br($f[text]);

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM