我尝试在将表单结果保存到CSV文件之前从表单结果中更改/删除/替换它。
基本过程:
我已尝试使用“\”包装数据,如此处所述>> Write to .csv file with PHP (Commas in Data Error)
这是我的基本形式> form.php的
<form action="thanks.php" method="post" >
<label for="Title">Title</label>
<select name="Title" class="form-control">
<option value=""> - select - </option>
<option value="Mr.">Mr.</option>
<option value="Mrs.">Mrs.</option>
</select>
<label for="FirstName">First Name *</label>
<input name="FirstName" type="text" value="" id="FirstName">
<label for="LastName"> Last Name *</label>
<input name="LastName" type="text" value="" id="LastName">
<label for="Email">Email Address *</label>
<input name="Email" type="text" value="" id="Email">
<label for="Comments">Comments</label>
<textarea name="Comments" rows="2" class="form-control" id="Comments"></textarea>
<label for="Description">Description</label>
<textarea name="Description" rows="2" class="form-control" id="Description"></textarea>
<button type="submit">Submit</button>
</form>
我的工作是什么:
thanks.php
<?php extract ($_POST); ?>
<?php
$Title = $_POST['Title'];
$FirstName = $_POST['FirstName'];
$LastName = $_POST['LastName'];
$Email = $_POST['Email'];
$Description = $_POST['Description'];
$Comments = $_POST['Comments'];
$fp = fopen("thedata.csv", "a");
$savestring =
$arr[] = "\"".$Title."\",".
$arr[] = "\"".$FirstName."\",".
$arr[] = "\"".$LastName."\",".
$arr[] = "\"".$Email."\",".
$arr[] = "\"".$Description."\",".
$arr[] = "\"".$Comments."\"".
"\n";
fwrite($fp, $savestring);
fclose($fp);
$fp = fopen("backupdata.csv", "a");
$savestring =
$arr[] = "\"".$Title."\",".
$arr[] = "\"".$FirstName."\",".
$arr[] = "\"".$LastName."\",".
$arr[] = "\"".$Email."\",".
$arr[] = "\"".$Description."\",".
$arr[] = "\"".$Comments."\"".
"\n";
fwrite($fp, $savestring);
fclose($fp);
?>
<?php
{
PRINT "<h1>Thanks for filling out the form</h1>";
}
$to = "email@address.com".",";
$subject = "Contact the client";
$message = "
<html><head><style>body,p,td{font-family:calibri; font-size:11pt; color: #000000}</style></head><body>
<table cellpadding=5>
<tr>
<td>Title</td>
<td>FirstName</td>
<td>LastName</td>
<td>Email</td>
<td>Description</td>
<td>Comments</td>
</tr>
<tr>
<td>$Title</td>
<td>$FirstName</td>
<td>$LastName</td>
<td>$Email</td>
<td>$Description</td>
<td>$Comments</td>
</tr>
</table>
</body>
</html>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "From:email@address.com" . "\r\n";
mail($to, $subject, $message, $headers);
?>
什么不起作用......
当我打开csv文件(在excel / sheets中)时,我注意到人们正在为描述和/或注释字段添加逗号......所以自动添加了多列。
我尝试在$ savestring中使用分号但是它甚至没有分成列。只有一行,我在描述中放了一个逗号,就在下一列的位置。
所以我知道我需要在写入CSV之前用连字符(或其他东西)替换逗号THEY,以便不创建列,从而将数据格式化为额外的列。
我无法弄清楚如何:|