我创建了一个PHP表单,将表单数据条目输出到一个单独的文本文件中。输出的问题是它是纯文本,条目之间没有换行符或空格。我想知道如何格式化输出数据,使其更清晰(可能使用表格或只是简单的换行符)。
我问我的教授一个解决方案,他把我推荐给this page.我确信这就是我要找的东西,但我很难根据自己的需要制作它。我需要从该页面上的代码更改以便它对我有用吗?我尝试过添加和减去代码,但这会导致语法错误。
这是PHP表单代码:
<?php
// define variables and set to empty values
$nameErr = '';
$emailErr = '';
$commentErr = '';
$likesErr = '';
$howErr = '';
$rateErr = '';
$name = '';
$email = '';
$comment = '';
$likes = '';
$how = '';
$rate = '';
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
if (empty($_POST["comment"])) {
$commentErr = "Comments are required";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["likes"])) {
$likesErr = "Things you liked is required";
} else {
$likes = test_input($_POST["likes"]);
}
if (empty($_POST["how"])) {
$howErr = "How you got to our site is required";
} else {
$how = test_input($_POST["how"]);
}
if (empty($_POST["rate"])) {
$rateErr = "Rating our site is required";
} else {
$rate = test_input($_POST["rate"]);
}
}
function resetForm($form) {
$form.find('input:text, input:password, input:file, select, textarea').val('');
$form.find('input:radio, input:checkbox')
.removeAttr('checked').removeAttr('selected');
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function file_write($data, $feedback){
if(is_string($data)){
return file_put_contents($feedback, $data, FILE_APPEND | LOCK_EX);//this appends the new data to the file and locks it while doing so to prevent multiple access to thje file at the same time.
}//return an error message if the data isnt a string
}
$data = $name.$email.$comment.$likes.$how.$rate.
'';
file_write($data, 'feedback.php')
?>
编辑1:
这是我认为导致问题的部分,因为有两个file_write
:
function file_write($data, $feedback){
if(is_string($data)){
return file_put_contents($feedback, $data, FILE_APPEND | LOCK_EX);
}//return an error message if the data isnt a string
}
$data = $name.PHP_EOL.$email.PHP_EOL.$comment.PHP_EOL.$likes.PHP_EOL.$how.PHP_EOL.$rate.PHP_EOL.
file_write($data, 'feedback.php');
答案 0 :(得分:2)
为了这个答案,我假设file_write
是你在某处包含的功能。 AFAIK file_write
不是用于写入文件的标准PHP函数。
您的数据作为单行写入文件,没有空格,因为您没有指定任何空格或换行符。
如果您希望在每个字段之间插入换行符,请执行以下操作:
$data = $name.PHP_EOL.$email.PHP_EOL.$comment.PHP_EOL.$likes.PHP_EOL.$how.PHP_EOL.$rate;
file_write($data, 'feedback.php');
最好使用PHP_EOL
添加换行符。它是跨平台的 - PHP将自动为您的代码运行的任何平台选择正确的换行符。
答案 1 :(得分:1)
尝试&#34; \ n &#34;或者您也可以尝试&#34; \ r \ n &#34; 所以你可以按照这个代码:
function file_write($data, $feedback){
if(is_string($data)){
return file_put_contents($feedback, $data.'\n\n', FILE_APPEND | LOCK_EX);
}//return an error message if the data isnt a string
}
$data = $name.'\n'.$email.'\n'.$comment.'\n'.$likes.'\n'.$how.'\n'.$rate;
答案 2 :(得分:1)
第一条评论。保留FILE_APPEND标志,因为它将保持正在进行的存储。
二。在将来,如果您想分析您的数据,将其与换行符分开,将很难解析。我建议只为新条目导出用换行符分隔的逗号。然后,您可以轻松地解析或导入(作为csv)到Excel中。
$data = $name.','.$email.','.$comment.','.$likes.','.$how.','.$rate.PHP_EOL;
file_write($data, 'feedback.php');
但是,您可能会遇到某人在您的网络表单中添加逗号的问题。你是如何解释的?您可以用引号将每个项目括在行中。你可以自己做那个!祝你好运。
答案 3 :(得分:1)
您可以使用PHP_EOL
常量,以便输出适合服务器的行结尾,在这种情况下,您的代码看起来像
$data = $_POST['name'] . PHP_EOL;
$data .= $_POST['email'] . PHP_EOL;
$data .= $_POST['comment'] . PHP_EOL;
$data .= $_POST['likes'] . PHP_EOL;
$data .= $_POST['how'] . PHP_EOL;
$data .= $_POST['rate'] . PHP_EOL;
或试试这个
$data = $_POST['name'] . "\r\n";
如果这不能帮你尝试这个
$data = sprintf("%s\n%s\n%s\n",$_POST['name'],$_POST['email'], $_POST['comment'],$_POST['how'],$_POST['rate']);
file_put_contents($file, $data, FILE_APPEND);
你可以试试这个:
<?php
// define variables and set to empty values
$nameErr = '';
$emailErr = '';
$commentErr = '';
$likesErr = '';
$howErr = '';
$rateErr = '';
$name = '';
$email = '';
$comment = '';
$likes = '';
$how = '';
$rate = '';
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
if (empty($_POST["comment"])) {
$commentErr = "Comments are required";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["likes"])) {
$likesErr = "Things you liked is required";
} else {
$likes = test_input($_POST["likes"]);
}
if (empty($_POST["how"])) {
$howErr = "How you got to our site is required";
} else {
$how = test_input($_POST["how"]);
}
if (empty($_POST["rate"])) {
$rateErr = "Rating our site is required";
} else {
$rate = test_input($_POST["rate"]);
}
}
function resetForm($form) {
$form.find('input:text, input:password, input:file, select, textarea').val('');
$form.find('input:radio, input:checkbox')
.removeAttr('checked').removeAttr('selected');
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function file_write($data, $feedback){
if(is_string($data)){
return file_put_contents($feedback, $data, FILE_APPEND | LOCK_EX);//this appends the new data to the file and locks it while doing so to prevent multiple access to thje file at the same time.
}//return an error message if the data isnt a string
}
$data = $name.PHP_EOL;
$data .= $email.PHP_EOL;
$data .= $comment.PHP_EOL;
$data .= $likes.PHP_EOL;
$data .= $how.PHP_EOL;
$data .= $rate.PHP_EOL;
file_write($data, 'feedback.php')
?>
希望这对你有所帮助