I am trying to change / strip / replace the COMMAS from the results of a form before it is saved to a CSV file.
The basic process:
- form filled out by a user. submit
- result written to a CSV file (open, write, close)
- email to an email address. (mail: to, subject, message, headers)
I have tried the wrapping of data with "\" as described here >> Write to .csv file with PHP (Commas in Data Error)
This is my basic form > 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>
What I have got working:
- Collecting of filled out form data to thanks.php
- Opening and writing to a csv file (well twice - ones a backup)
- Sending of forms data as html to an email
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."\"".
"
";
fwrite($fp, $savestring);
fclose($fp);
$fp = fopen("backupdata.csv", "a");
$savestring =
$arr[] = "\"".$Title."\",".
$arr[] = "\"".$FirstName."\",".
$arr[] = "\"".$LastName."\",".
$arr[] = "\"".$Email."\",".
$arr[] = "\"".$Description."\",".
$arr[] = "\"".$Comments."\"".
"
";
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" . "
";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "
";
$headers .= "From:email@address.com" . "
";
mail($to, $subject, $message, $headers);
?>
What is NOT working...
When I open the csv file (in excel/sheets), I noticed that people are adding commas to the description and / or comments fields... so there are extra columns automatically.
I have tried using semi-colons instead in $savestring but then it doesn't even separate into columns. Just one line, and where I put a comma in description that's where the next column comes.
So I know I need to replace the commas THEY type in with hyphens (or something else) BEFORE I write to CSV so that columns are NOT created, therefore breaking the formatting of data into extra columns.
I just can't figure out how :|