2014-03-27 40 views
-1

我正在製作一個網站,稍後在世界上發佈,但我有一個表單,我要發送一個然後一個php腳本來處理所述表單,但是在我寫完腳本之後。 txt文件我不知道如何讓它放在一個分頁符或輸入,以便多個表單條目不在同一行,任何人都不知道如何解決這個問題? PHP代碼看起來像這樣在php中寫一個換行符

<?php 
$username = $_POST['username'] ; 
$email=$_POST['mail']; 
$comment = $_POST['message']; 
//the data 
$data = "$username | $email | $comment "; 
//open the file and choose the mode 

$fh = fopen("comments.txt", "a"); 

fwrite($fh, $data,); 
//close the file 
fclose($fh); 
print "Message Sent Successfully! Hit the back button for more skubduger.tk"; 
?> 

回答

1
//the data 
$data = "$username | $email | $comment \n"; 
+0

試過這個,但它只是放一個空格,然後寫下一個提交。有什麼我做錯了,當我安裝php – user3466945

+0

\ r \ n? \ n通常爲我工作 – dian

0

既然你寫一個文件,你需要換行字符,\n添加到您的數據的末尾。當您創建的變量,你可以做到這一點,或者當你將其寫入文件(或任何地方之間真的):

$data = "$username | $email | $comment \n"; 

或使用sprintf(),使其乾淨了一點(至少,清潔我= P):

$data = sprintf("%s | %s | %s \n", $username, $email, $comment); 

,或者直接添加它來$data當你將其寫入文件:

fwrite($fh, $data . "\n"); 

如果你希望它是Windows的SPECI fic,你會想要回車+換行,\r\n

+0

由於一些奇怪的原因,沒有建議工作,但\ r \ n不知道爲什麼,但我現在很高興,並感謝您的專業知識 – user3466945

+0

@ user3466945如果'\ r \ n'工作,這很可能意味着你在Windows上,而你正在嘗試在記事本中讀取該文件;在Linux上或在Windows上的其他文本編輯器(如Notepad ++)中,單個'\ n'也可以實現這一功能。 – newfurniturey

0

您可以使用PHP_EOL常量而不是猜測要使用的\ r \ n或\ n。它可以在任何平臺上運行。

$data = "$username | $email | $comment ".PHP_EOL;