Page 1 of 1

Help writing form data to CSV file PLZ

Posted: Mon Dec 01, 2008 8:19 pm
by npodach
~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: :arrow: 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: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Help writing form data to CSV file PLZ

Posted: Tue Dec 02, 2008 12:47 am
by requinix
Pretty simple. Use fopen to open the file, fputcsv to write to it, and fclose to close it.

Re: Help writing form data to CSV file PLZ

Posted: Tue Dec 02, 2008 6:32 am
by npodach
tasairis wrote:Pretty simple. Use fopen to open the file, fputcsv to write to it, and fclose to close it.
Yes, I've read that in a few different tutorials but I am unsure as to the formatting of the write to it part. How do I make it write the data from the Name and Email Address form fields and make sure it is comma separated, and starts a new line? Could you give me a simple example? I am very confused and nothing I've done so far has actually written the data. Thank you for your help so far!

Re: Help writing form data to CSV file PLZ

Posted: Wed Dec 03, 2008 6:23 am
by npodach
Please somebody help! If it's not that hard then it will only take a few minutes for you to explain it to me! :)

Re: Help writing form data to CSV file PLZ

Posted: Wed Dec 03, 2008 6:38 am
by mintedjo
Post your attempt at writing the csv xD It's better than somebody just telling you the answer because they might be able to tell you where you are going wrong and then you learn more. :-)

Re: Help writing form data to CSV file PLZ

Posted: Wed Dec 03, 2008 11:03 am
by npodach
Ok but I don't understand this completely so I could be wayyy off! I don't get any errors but it's not writing to the file either. I tried, will you please help me now? :D

Here's what I have right now:

Code: Select all

$fh = fopen("home/sub/test.csv", "a");
$csvfile = $Name . ',' . $EmailAddress . "\n";
fwrite($fh, $csvfile);
fclose($fh);
 

Re: Help writing form data to CSV file PLZ

Posted: Wed Dec 03, 2008 11:16 am
by mintedjo
Your code works fine for me.
Check that you are writing the right directory - maybe change the path to just "test.csv" so you know where it is.
Check the permissions on the folder you are writing to.

And don't use " and '. pick either " or ' and stick with it.