doujizhong8352 2019-03-05 01:21
浏览 305

如何在html textarea POST数据中保留换行符

I am trying to have lyrics to a song submitted into a text area. So, this needs to keep the line-breaks when I output. I am aware of the fact that browsers standard should use as the line-break, and PHP's nl2br() function can convert the into <br>tags.

I am also aware of the fact that textarea inputs require wrap="hard" in order to pass the line-breaks, otherwise the default of wrap="soft" will ignore, and that cols="" must be specified when using wrap="hard". I have attempted to accomplish this with the html below:

<textarea wrap="hard" name="reading_text" cols="450" rows="6" maxlength="5000"></textarea>

However, the POST array does not show any line breaks. I enter this into the textarea:

Line one of the lyrics

That were entered into the textarea

Alas to my dismay

Red eyes and fire and signs

Because the line-breaks are not in the POST data

I want to make a ray of sunshine and never leave home

I get this in the POST array:

array(5) { ["reading_text"]=> string(216) "Line one of the lyrics That were entered into the textarea Alas to my dismay Red eyes and fire and signs Because the line-breaks are not in the POST data" ["add_reading"]=> string(3) "Add" }

To make matters more confusing for me I tried to look at W3 Schools demo (https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_textarea_wrap). However, I cannot seem to even make that example work. When I enter many line breaks into the textarea, the text comes out with no .

What am I missing here?

  • 写回答

3条回答 默认 最新

  • duanhui1869 2019-03-05 02:52
    关注

    You could pass the textarea text as part of the querystring. This will force the entire string to be url encoded like this:

    %0AGray%2C%20quiet%20and%20tired%20and%20mean%0A%0APicking%20at%20a%20worried%20seam%0A%0AI%20try%20to%20make%20you%20mad%20at%20me%20over%20the%20phone.%0A%0ARed%20eyes%20and%20fire%20and%20signs%0A%0AI%27m%20taken%20by%20a%20nursery%20rhyme%0A%0AI%20want%20to%20make%20a%20ray%20of%20sunshine%20and%20never%20leave%20home%0A
    

    then just decode the string on the receiving page with urldecode(). This should keep the formatting and display it correctly.

    http://php.net/manual/en/function.urldecode.php

    https://www.tools4noobs.com/online_php_functions/urldecode/

    I suppose you could just also urlencode() the textarea text before POSTING and urldecode it on the other end.

    评论

报告相同问题?

悬赏问题

  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误