[英]Preserving line breaks from textarea when applying new styles to the text
[英]Send textarea data to a file using JS & PHP and preserving line breaks
我有一個表格,其中有一個文本區域,顯示讀取文件(通過PHP)的結果; 我要它編輯並按一個按鈕,然后將其存儲回來
<?php
$salida = file_get_contents($path);
echo "<textarea rows='10' cols='100' id='file' name='file'>" . "$salida" . "</textarea>";
?>
保存方式(JS)
document.getElementById('btn-save').addEventListener('click', function(e) {
e.preventDefault();
request({
method: 'GET',
url: 'actions/save.php?file='+document.getElementById('file').value
}, function(ok) {
...
}, function(ko) {
...
});
});
save.php
<?php
$TEMP = '/tmp/file.tmp';
if (isset($_GET['file']))
{
$fh = fopen($TEMP, "w+");
$string = $_GET['file'];
fwrite($fh, $string); // Write information to the file
fclose($fh); // Close the file
}
該文件是這樣的:
line1=a
line2=b
line3=c
問題是:讀取時正確顯示所有行; 但是保存時(上述方法),文件顯示如下:
line1=aline2=bline3=c
我需要保留文件中的中斷嗎?
提前致謝
您可以在該文件的輸出上使用內置的nl2br()函數:
<?php
$salida = file_get_contents($path);
echo "<textarea rows='10' cols='100' id='file' name='file'>" . nl2br($salida) . "</textarea>";
?>
您應該使用POST方法而不是GET。 如果您的文件將包含更多的GET限制,那么您丟失了內容。
document.getElementById('btn-save').addEventListener('click', function(e) {
e.preventDefault();
request({
method: 'POST',
url: 'actions/save.php',
data: {content: document.getElementById('file').value}
}, function(ok) {
...
}, function(ko) {
...
});
});
和PHP文件
if (isset($_POST['content']))
{
$fh = fopen($TEMP, "w+");
$string = $_POST['content'];
fwrite($fh, $string); // Write information to the file
fclose($fh); // Close the file
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.