In contenteditable div, linebreak ignored when enter key is pressed

后端 未结 2 444
醉话见心
醉话见心 2021-01-29 09:07

When I press the return key to start a new line for the post, the result auto-ignore the return key. I wondered what the most common way of making the output actually starts wi

相关标签:
2条回答
  • 2021-01-29 09:38

    No. The <br /> is not at all present there. There's \r\n. So you need to use:

    $comment = nl2br($comment);
    

    To convert all the new lines to break in your PHP code.

    0 讨论(0)
  • 2021-01-29 09:57

    Im replying to this because your other question was marked duplicate.

    By replacing 2 of your <div> with these,

    <form>
            <textarea name="comment_box" id="comment_box"></textarea>
            <input id="post_comment" class="comment_leg" type="submit" value="Post">
    </form>
    

    It is able to capture the space by changing your script to capture the textarea with the method .val()

    var txt = $("textarea#comment_box").val();
        if(txt){
            $.post("commenttest.php", {txt: txt}, function(result){
                $("div[name=commentsubmit]").prepend(result);
                $("textarea#comment_box").val();
            });
        }
    

    To have it printed with the line break, in your commenttest.php, insert this before you echo, which is what you have tried but didnt work from your duplicate post or recommended from an answer.

    $comment = nl2br($comment);
    
    0 讨论(0)
提交回复
热议问题