HTML textarea input doesnt recognize line breaks

℡╲_俬逩灬. 提交于 2019-12-02 22:41:17

问题


I have a really silly problem, but i cannot find a way to solve it, I programming a helpdesk system, with support Tickets I have a form in my website like

<form action="somepage.php" action="post">

<input id="reply" type="textarea">

<button id="replyButton" type="submit">Submit</button>

</form>

but when the user submits the text, and i show it in the website as the conversation between the user and the support assistant, it doesnt recognize the line breaks, i dont know if its putting \n\r or #10#13, or even nothing...

i've tested and when i press enter in the text area and click submit, when i get that same text from my database i get EMPTY SPACES where the linebreaks should be...

BTW> before displaying the text im using utf8_enconde($text) because people are typing speciales characters like letters with acute and ñ


回答1:


In HTML you use the

<br>

tag for line breaks. Replace all line breaks (newline characters) with said tag and you should be fine. You can "convert" line breaks (newline characters) to that tag using nl2br() (it really only insert such tags after line breaks (newline characters).




回答2:


Escape all special characters before storing it into the database. For instance in MySQL, you can use mysql_real_escape_string. This escapes all special characters while storing in the Database.

mysql_real_escape_string : http://php.net/manual/en/function.mysql-real-escape-string.php

Then use nl2br() while displaying back the content from Database.

nl2br() : http://php.net/manual/en/function.nl2br.php



来源:https://stackoverflow.com/questions/19581393/html-textarea-input-doesnt-recognize-line-breaks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!