问题
My php form which saves the output in a text file glues the result to one string like:
Name1Email1The Message1Name2Email2The Message2Name3Email3The Message3Name4Email4The Message4
But I need spaces and/or newlines. I normaly don't use PHP so I don't get it. I did't find an answer on the web, also read some Q/A here, but this didn't help me.
The Form:
<form action="mailer.php?savedata=1" method="post">
Your Name: <input type="text" name="name"><br>
Your Email: <input type="text" name="email"><br>
Your Message:<br> <textarea name="message" rows="5" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
The PHP
<?php
$savedata = $_REQUEST['savedata'];
if ($savedata == 1){
$data = $_POST['name'];
$data .= $_POST['email'];
$data .= $_POST['message'];
$file = "YOURDATAFILE.txt";
$fp = fopen($file, "a") or die("Couldn't open $file for writing!");
fwrite($fp, $data) or die("Couldn't write values to file!");
fclose($fp);
echo "Your Form has been Submitted!";
}
?>
回答1:
As other answers state, you need to add in an end of line character after each field.
Different OS's use different line endings, though, and so a "\n" may not display as a new line on Windows, for example. As Mahdi said, you can use Windows style "\r\n" line endings, or you can use the PHP_EOL constant so that line endings appropriate to the server will be output, in which case your code would look like
$data = $_POST['name'] . PHP_EOL;
$data .= $_POST['email'] . PHP_EOL;
$data .= $_POST['message'] . PHP_EOL;
回答2:
You can use "\n" to write a new line.
$data = $_POST['name'] . "\n";
$data .= $_POST['email'] . "\n";
$data .= $_POST['message'] . "\n";
Just as a sidenote, \n has to be in doublequotes. When surrounded by single quotes it won't work.
回答3:
When you concatenate your input values like that you won't have any newlines; you can add them using the literal "\n"
or "\r\n"
. To string them all together you can use the concatenation operator or use sprintf()
like below:
$data = sprintf("%s\n%s\n%s\n", $_POST['name'], $_POST['email'], $_POST['messge']);
$file = "YOURDATAFILE.txt";
file_put_contents($file, $data, FILE_APPEND);
I also use file_put_contents()
with the FILE_APPEND
flag to append data to a file and save a few more lines of code.
回答4:
I usually combine New Line
with Carriage Return
:
$data = $_POST['name'] . "\r\n";
回答5:
Check the filter that you're using to clean form input. It may be removing the 0x0a NewLine characters from your form input.
I have text saved from a 'textarea' tag and it's correct. Using a HEX viewer, I see that each newline from the "textarea" field gets translated to hex character 0a. (decimal 10) Wikipedia lists this as the UTF-8 "newline character" or "LF." LF was short for Line Feed, from the old teletype days. https://en.wikipedia.org/wiki/UTF-8
So, the lines test test test translate to "4 65 73 74 0d 0a 74 65 73 74 0d 0a 74 65 73 74" Note the two "0a" characters in the string, which are equivalent to \n.
This wasn't apparent at first because the regular expression I was using to clean the input was replacing the 0a character with a space.
回答6:
you'r $data
variable needs to already have the \n
new line characters in it.
回答7:
this works for me:
echo "$data"."\r\n";
回答8:
When we attempt to extract data submitted online via a Web-Form and to write the same to a text file which is placed on the web-server -- without overwriting the information already contained in the said text file, then we use the following code within the tags:
<html>
<body>
<form action="" method="">
<input value="submit">
<div>Name: <input value=""></div>
<div>email: <input value=""></div>
... ... ...
<div><button>Submit</button></div>
</form>
</body>
</html>
The above HTML Code is coupled with the following PHP Code:
<?php
$fileName = 'fdata.txt';
$fp = fopen('fdata.txt', 'a');
$savestring = $_POST['name'] . ',' . $_POST['email'] ;
fwrite($fp, $savestring);
fclose($fp);
?>
But the above two Codes together lead to data that is appended and stringed together in the text file -- that is to say, every new record (set of data) extracted from subsequent Web-Forms does not begin at a new line (there is no line-break or carriage-return).
I searched the Net for a Solution to the above Issue -- as suggested on the Net, I added “\r\n” (in double quotes) and also used ‘PHP_EOL’ (without quotes) as under:
$fp = fopen('fdata.txt', 'a', "\r\n");
$savestring = “\r\n” . PHP_EOL . ' ' . $_POST['name'] . ',' . $_POST['email'] .
PHP_EOL;
Yet there was no line-break at all (my web-server and my desktop use Windows). It means that there was no practicable Solution within the knowledge of anyone of the millions of Users of Net in the World. It appears that the parameter ‘a’ overrides “\r\n”, ‘PHP_EOL’ etc.
来源:https://stackoverflow.com/questions/12908334/new-line-in-php-output-in-a-text-file