简体   繁体   中英

PHP print newline into textarea

I'm trying to print newline into textarea.

I have this string:

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

When I use the following javascript command:

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: 在此处输入图片说明

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

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:

hi Masume
have a nice day!

With PHP:

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

With JS:

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

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