1
我很新的PHP。我有一個FORM輸入數據,FORM包含3個字段:名稱,年齡,地址 我想獲取提交的數據,並將它們逐行寫入文本文件。 例如,如果我在表單提交3次,我會在下面的文本文件:如何在PHP中逐行寫入文本文件?
john
20
US
Simson
18
UK
Marry
26
Japan
I tried to implement it, but there was always a blank space in the beginning of text file, or there was always a blank space in the end of text file.I could not write file line by line. How do I do that, please help me ?
here is my form :<br>
<form action="themSinhVien.php" method="POST">
<table id="tableHome">
<tr>
<td id="td1">
<span> Name: </span>
</td>
<td id="td2">
<input type="text" name="name" placeholder="put your name here">
</td>
</tr>
<tr>
<td id="td1">
<span> Age: </span>
</td>
<td id="td2">
<input type="text" name="age" placeholder="put your age here">
</td>
</tr>
<tr>
<td id="td1">
<span> Adress: </span>
</td>
<td id="td2">
<input type="text" name="address" placeholder="put your address here">
</td>
</tr>
<tr>
<td id="td1" style="padding-left:180px">
<input type="submit" name="submit" value="Submit">
</td>
<td id="td2">
<input type="reset" name="reset" value="reset">
</td>
</tr>
</table>
</form>
這裏是我的PHP腳本:
<?php
if(isset($_POST['submit'])){
$name = "\r\n".$_POST['name'];
$age = "\r\n".$_POST['age']."\r\n";
$address = $_POST['address'];
$file = fopen("student.txt","a",1);
fwrite($file,$name);
fwrite($file,$age);
fwrite($file,$address);
fclose($file);
echo "Adding student successfully";
}
?>
你的代碼幾乎可以工作嗎? – AbraCadaver
如果您打開文本文件,修剪內容可能會很好 –
我添加了我的代碼。文件寫入開始時總是有一個空行。那麼如何修剪內容? –