[英]How to save textarea content inside a .txt file with php
我是php的初學者,我正在嘗試將textarea內容保存在news.txt文件中。
保存后,我將在另一個頁面中顯示結果。 一切正常,日期和標題完美顯示,甚至是textarea內容,但如果按Enter創建新行,它將不再起作用。
這是發生的事情和一些代碼。
注意:未定義的偏移量:第17行的C:\\ Virtual Servers \\ xampp \\ htdocs \\ read.php中的1
注意:未定義的偏移量:第18行的C:\\ Virtual Servers \\ xampp \\ htdocs \\ read.php中的2
和我的代碼:
read.php頁面
<html>
<head>
<style>
.news{background:#f1f1f1;width:960px;height:500px;margin-bottom:15px;}
.title{color:red;}
.date{font-style:italic;}
.text{color:green;font-weight:bold;}
</style>
</head>
<body>
<?php
$i = 0;
$array = file("news/news.txt");
foreach($array as $row){
$split = explode('|', "$row");
$data = $split[0];
$titolo = $split[1];
$testo = $split[2];
$i++;
print '<div id="'.$i.'" class="news"><div class="date">'.$data.'</div><div class="title">'.$titolo.'</div><div class="text">'.$testo.'</div></div>';
}
?>
</body>
</html>
index.php頁面:
<div class="container">
<form name="myform" onsubmit="return validateForm()" action="welcome.php" method="post">
<?php echo date('d-m-Y'); ?></br>
<h1>Titolo:</h1><input type="text" id="Title" name="titolo" class="textStyleTitle"><br>
<p>Testo:</p><textarea id="Text" name="testo" class="textStyleText"></textarea><br>
<input type="submit" value="salva">
</form>
</div>
有什么辦法嗎?
非常感謝。
!! UPDATE! 使用下面提到的解決方案,我已經部分解決了。 我沒有錯誤,但是看起來是這樣的:
<body>
<div id="1" class="news">
<div class="date">04-01-2014</div>
<div class="title">Testing title</div>
<div class="text">Testing textarea row 1 (enter pressed)</div>
</div>
<div id="2" class="news">
<div class="date">Testing textarea row 2 (enter pressed)</div>
<div class="title"></div><div class="text"></div></div>
<div id="3" class="news">
<div class="date">Testing textarea row 3 (enter pressed)</div>
<div class="title"></div><div class="text"></div></div>
<div id="4" class="news">
<div class="date">Testing textarea row 4</div>
<div class="title"></div><div class="text"></div>
</div></body>
不幸的是,我試圖達到的是這樣的:
<body>
<div id="1" class="news">
<div class="date">04-01-2014</div>
<div class="title">Testing title</div>
<div class="text">Testing textarea row 1 (enter pressed)
Testing textarea row 2 (enter pressed)
Testing textarea row 3 (enter pressed)
Testing textarea row 4
</div>
</div>
首先在$ explode中刪除$ row的引號,如下所示: $split = explode('|', $row);
然后
采用:
$data = (isset($split[0])?$split[0]:"");
代替
$data = $split[0];
$split[1]
和$split[2]
您在這里犯了一個錯誤 : $split = explode('|','$row')
,無需將$row
設置為引號,因為它將使用它作為string
而不是數組
您需要使用$split = explode('|',$row)
將字符串分解為數組
試試這個代碼。
foreach($array as $row){
$data = $titolo = $testo = "";// initialize the valiables
$split = explode('|', $row);
$data = (isset($split[0])?$split[0]:"");
$titolo = (isset($split[1])?$split[1]:"");
$testo = (isset($split[2])?$split[2]:"");
$i++;
print '<div id="'.$i.'" class="news"><div class="date">'.$data.'</div><div class="title">'.$titolo.'</div><div class="text">'.$testo.'</div></div>';
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.