I'm using below code to get data from PHP form. Its working fine.
<?php
$email=$_POST['email'];
$name=$_POST['name'];
$designation=$_POST['designation'];
$company=$_POST['company'];
$phone=$_POST['phone'];
$data=$name.",".$email.",".$designation.",".$company.",".$phone;
$file="file.csv";
file_put_contents($file, $data . PHP_EOL, FILE_APPEND);
?>
Whenever any user puts information in the form with separated (.) then name and email come in different cells in .csv file.
How it will solved?
Thanks!
</div>
Use PHP's fputcsv
. Let it handle the particulars of CSV formatting for you!
<?php
$email = $_POST['email'];
$name = $_POST['name'];
$designation = $_POST['designation'];
$company = $_POST['company'];
$phone = $_POST['phone'];
$data = [
$name,
$email,
$designation,
$company,
$phone,
];
$file = fopen('file.csv', 'a'); // Append data to end of file.
fputcsv($file, $data);
If you need a delimiter other than the default (,
) put that for the third argument. Same for enclosure & escape chars ("
, \
) as 4th and 5th args.