Help writing form data to CSV file PLZ
Posted: Mon Dec 01, 2008 8:19 pm
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
I have a HTML form that collects data through several fields. The PHP code below takes that data and emails it to someone and then sends an autoresponse back to the customer using the name and email address that they entered. All of that works fine (although I'm sure it could be coded in a better manner). My question is can I also take the submitted name and email address and write it to a CSV file as well? I am extremely new to PHP and I have been trying to do this without success for hours. What do I need to do or add to get it to do this? I already have the file test.csv uploaded in a separate folder and the permissions set. Any help you could give me would be greatly appreciated!!
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
I have a HTML form that collects data through several fields. The PHP code below takes that data and emails it to someone and then sends an autoresponse back to the customer using the name and email address that they entered. All of that works fine (although I'm sure it could be coded in a better manner). My question is can I also take the submitted name and email address and write it to a CSV file as well? I am extremely new to PHP and I have been trying to do this without success for hours. What do I need to do or add to get it to do this? I already have the file test.csv uploaded in a separate folder and the permissions set. Any help you could give me would be greatly appreciated!!
Code: Select all
<?php
/* Set e-mail recipient */
$myemail = "myemail@mydomain.com";
$subject = "Transfer Request";
/* Check all form inputs */
$TransferType = check_input($_POST['TransferType']);
$Name = check_input($_POST['Name'], "Enter your name");
$ResidenceAddress = check_input($_POST['ResidenceAddress'], "Enter your Residence Address");
$MailingAddress = check_input($_POST['MailingAddress']);
$EmailAddress = check_input($_POST['EmailAddress'], "Enter your Email Address");
$DaytimePhone = check_input($_POST['DaytimePhone']);
$Manufacturer = check_input($_POST['Manufacturer']);
$Model = check_input($_POST['Model']);
$AdditionalComments = check_input($_POST['AdditionalComments']);
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $EmailAddress))
{
show_error("E-mail address not valid");
}
/* Prepare the message for the e-mail */
$message = "
A transfer request has been submitted by:
Type of Transfer: $TransferType
Name: $Name
Residence Address: $ResidenceAddress
Mailing Address: $MailingAddress
Email Address: $EmailAddress
Daytime Phone: $DaytimePhone
Manufacturer: $Manufacturer
Model: $Model
Additional Comments: $AdditionalComments
";
/* Prepare an autoreply */
$automessage = "Hello $Name,
Thank you for submitting a transfer request. WE WILL CONTACT YOU as soon as we receive and process your request.
";
/* Send the messages */
mail($myemail, $subject, $message);
mail($EmailAddress, $subject, $automessage, 'From: myemail@mydomain.com');
/* Redirect visitor to the thank you page */
header('Location: thankyou.htm');
exit();
/* Functions used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: