简体   繁体   English

PHP将换行符打印到textarea中

[英]PHP print newline into textarea

I'm trying to print newline into textarea. 我正在尝试将换行符打印到textarea中。

I have this string: 我有这个字符串:

$coords = $row->lat.','.$row->lon."\\r\\n"

When I use the following javascript command: 当我使用以下javascript命令时:

alert(coords); 

I get: 我得到:

-35.308401,149.124298
-35.307841,149.124298

However, when I view it in a textarea, I get: 但是,当我在文本区域中查看它时,我得到:

-35.308401,149.124298 -35.307841,149.124298

How do I make the newlines display in the textarea? 如何使换行符显示在文本区域中?

More Information: 更多信息:

alert window display: 警报窗口显示:

警报窗口

textarea: textarea的: 在此处输入图片说明

Code: 码:

<textarea name="addrs" rows=5 cols=80>-35.308401,149.124298 -35.307841,149.124298</textarea>

Further Information: This is how form is created and text written to textarea 更多信息:这是创建表格并将文本写入textarea的方式

function openMapWindow (data) {
    alert(data);

    var mapForm = document.createElement("form");
    mapForm.target = "Map";
    mapForm.method = "POST"; // or "post" if appropriate
    mapForm.action = "http://www.xxx.com/map.php";

    var mapInput = document.createElement("input");
    mapInput.type = "text";
    mapInput.name = "addrs";
    mapInput.value = data;
    mapForm.appendChild(mapInput);

    document.body.appendChild(mapForm);

    map = window.open("", "Map", "status=0,title=0,height=600,width=800,scrollbars=1");

    if (map) {
        mapForm.submit();
    } else {
        alert('You must allow popups for this map to work.');
    }

}

function mapSuppliers(customer_id) {
    $.get("get.map.points.php", { c_id: customer_id },
     function(data){
         if (data!='') {
            openMapWindow(data);
         } else {
             alert("Missing map coordinates - cannot display map (data: " + data + ")");
         }
     });

}

You are trying to have multiple rows in an input field. 您试图在输入字段中包含多行。

Consider updating your code to: 考虑将代码更新为:

var mapTextarea = document.createElement("textarea");
mapTextarea.name = "addrs";
mapTextarea.value = data;
mapForm.appendChild(mapTextarea);

I know this question is quite old already, but I will post my answer just in case anyone comes here. 我知道这个问题已经很老了,但是我会发布我的答案,以防万一有人来。

For example, if we would like to show this text in textarea: 例如,如果我们想在textarea中显示此文本:

hi Masume
have a nice day!

With PHP: 使用PHP:

echo "hi Masume".chr(13)."have a nice day!";

With JS: 使用JS:

textareaElement = 'hi Masume' + String.fromCharCode(13) + 'have a nice day!';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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