posting form data to a CSV file with embedding Carraige returns or commas


#1

Hi everyone,

I am comparative newbie to the world of PHP and am managing a site for client which has meant I have had to build a form and capture the data to a .csv file

My form can be view here: http://comecourting.co.uk/feedback/?page_id=32

You will see that in addition to normal text fields there are half a dozen text area fields

the processing of this data is managed by the following script:

[php]<?
$username= check_input($_POST[‘textfield’]);
$Password= check_input($_POST[‘TXT_Password’]);
$title= check_input($_POST[‘TXT_title’]);
$forename= check_input($_POST[‘TXT_forename’],“Click Back on your browser and Enter your name!”);
$surname=check_input($_POST[‘TXT_surname’],“Click Back on your browser and Enter your name!”);
$gender=check_input($_POST[‘TXT_gender’]);
$dob= check_input($_POST[‘TXT_DOB’],“Click Back on your browser and Enter your Date Of Birth!”);
$telephone= check_input($_POST[‘TXT_Telephone’],“Click Back on your browser and Enter your Telephone Number!”);
$email= check_input($_POST[‘TXT_email’]);
$address1= check_input($_POST[‘TXT_address1’]);
$address2= check_input($_POST[‘TxT_address2’]);
$town= check_input($_POST[‘TxT_Town’],“Click Back on your browser and Enter your TOWN!”);
$county= check_input($_POST[‘TxT_County’]);
$postcode= check_input($_POST[‘TxT_Postcode’]);
$event= check_input($_POST[‘TXT_event’]);
$agegroup= check_input($_POST[‘TXT_agegroup’]);
$Describeyourself= check_input($_POST[‘TXT_describeyourself’]);
$Hobbies= check_input($_POST[‘TXT_hobbies’]);
$partnercharacter= check_input($_POST[‘TXT_partnercharacter’]);
$idealday= check_input($_POST[‘TXT_idealday’]);
$friends= check_input($_POST[‘TxT_friends’]);
$source= check_input($_POST[‘TxT_source’]);
$Optin= check_input($_POST[‘Optin’]);

function check_input($data, $problem=’’)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
die($problem);
}
return $data;
}

$fh = fopen(“Profiles.csv”, “a”);
$savestring = “\n” . $username . “,” . $Password . “,” . $title . “,” . $forename . “,” . $surname . “,” . $gender . “,” . $dob . “,” . $telephone . “,” . $email . “,” . $address1 . “,” . $address2 . “,” . $town . “,” . $county . “,” . $postcode . “,” . $event . “,” . $agegroup . “,” . $Describeyourself . “,” . $Hobbies . “,” . $partnercharacter . “,” . $idealday . “,” . $friends . “,” . $source . “,” . $Optin;
fwrite($fh, $savestring);
fclose($fh);

?>
[/php]

this is working quite sucessfully except where the user puts commas or carriage returns in the the text area field.

If they put a comma in - it creates a new field on the csv file and knock the data out of synch
If they put a carriage return in in creats a new record on the csv file.

does anyone know a clever way to prevent this?

All help would be greatly appreciated

regards

Sandyh2012