How to?? Save web form info to csv....im a bit stuck :)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lazynewt
Forum Newbie
Posts: 15
Joined: Mon May 19, 2008 10:43 am

How to?? Save web form info to csv....im a bit stuck :)

Post by lazynewt »

~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.


:banghead:
Hi im trying to move information from a web form (entered by user) through to a database. the flow at the moment is

user enters details> form passes info to process.php > process emails to outlook > info is manually copied and pasted into empty csv file and imported into db.

at the moment im using this in my process.php

Code: Select all

 
function sendApplicationDataEmail() {
 
   global $bodyHeadingApplication;
   global $subjectApplication;
   global $emailTo;
   $emailFrom = _cleanUp($_REQUEST['appEmail']);
   $emailReplyTo = _cleanUp($_REQUEST['appEmail']);
   $lineEnd = "\n";
 
   $trimmedReferrerID = returnID($_SESSION['referrerID']);
 
 
   // create message body
   $messageBody  = $bodyHeadingApplication.$lineEnd."----------------------------------------".$lineEnd.$lineEnd;
 
   $messageBody .= "Marital Status : "._cleanUp($_REQUEST['appMarital']).$lineEnd.$lineEnd;
   $messageBody .= ""._cleanUp($_REQUEST['appTitle']);
   $messageBody .= ","._cleanUp($_REQUEST['appFirstNames']);
   $messageBody .= ","._cleanUp($_REQUEST['appSurname']);
   $messageBody .= ","._cleanUp($_REQUEST['appDOB']);
   $messageBody .= ","._cleanUp($_REQUEST['appAddress']);
   $messageBody .= ","._cleanUp($_REQUEST['appTownCity']);
   $messageBody .= "County : "._cleanUp($_REQUEST['appCounty']).$lineEnd.$lineEnd;
   $messageBody .= ","._cleanUp($_REQUEST['appPostCode']);
 
   $result = @mail("$emailTo","$subjectApplication","$messageBody","From: $emailFrom\r\nReply-to: $emailReplyTo");
   if (!$result) {
      return "SYSTEM ERROR: sign in form submission failed!";
   }
   else {
      sendAcceptAutoEmail();
      sendReferrerEmail();
      return "Thanks stopping by .";
   }
}
My only problem is i could do with outputting the info to a seperate csv file and attaching that to save doing this manually. im just a bit stuck with how to do this. My only other alternative is to process it at this end using some type of batch processing script/app.... My output in email has carriage returns which is a pain in the posterior and looks like this.
Title,First Names,Surname,Date of Birth,Street Address,Town/City,Post Code
this is non-profit just trying to help a friend out and im starting to think im in over my head lol. Any advice is appreciated. :banghead:


~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.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: How to?? Save web form info to csv....im a bit stuck :)

Post by Jade »

Wouldn't it be easier to have the php just enter the data directly into the database instead of going through the hassle of converting to CVS and then uploading?? That doesn't make sense to me. Are you storing the CVS file for something?
lazynewt
Forum Newbie
Posts: 15
Joined: Mon May 19, 2008 10:43 am

Re: How to?? Save web form info to csv....im a bit stuck :)

Post by lazynewt »

Hi Jade thanks for the reply. I am eventually going to have the db converted to mysql but at the moment its an access locally stored db. Thats why im a bit stuck. I know its not the best way to do this but its all ive got to work with right now. :crazy:
lazynewt
Forum Newbie
Posts: 15
Joined: Mon May 19, 2008 10:43 am

Re: How to?? Save web form info to csv....im a bit stuck :)

Post by lazynewt »

Ok i figures id be using fopen/fwrite and am testing on a txt file. This is the code i have so far.

Code: Select all

$filename = 'test.txt';
$somecontent = _cleanUp($_REQUEST['appTitle']).","._cleanUp($_REQUEST['appFirstNames']).","._cleanUp($_REQUEST['appSurname']).","._cleanUp($_REQUEST['appDOB']).","._cleanUp($_REQUEST['agreeConfirm'])." - .";
 
// Making sure the file is writable.
if (is_writable($filename)) {
 
    // opening $filename in append mode.
    // The file pointer is at the bottom of the file this is where $somecontent is going to go.
    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }
 
    // Write $somecontent to our opened file.
    if  (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }
 
    echo "Thank you for your application";
 
    fclose($handle);
 
} else {
    echo "The file $filename is not writable";

ok so i am writing to the file no problem i just need to write onto a new line each time because at the moment its writing one continuous line of text which isnt what access/excel wants. Im assuming i can use a

Code: Select all

.$lineEnd
just not sure where? Then my next task is to get it to output to .csv instead (not a problem) and then email it to me.

any help would be appreciated as you can see im really trying to fix this myself and not just begging for code.

thanks
============================UPDATE=================================================
actually i am now thinking of creating a new file for each user. Then transferring and importing every few hours.... :drunk:

===================UPDATE 2 =======================

the error i get is " The file Mr,aaaarglefargle,testerton, is not writable" it is a result of using this code

Code: Select all

$filename = _cleanUp($_REQUEST['appTitle']).","._cleanUp($_REQUEST['appFirstNames']).","._cleanUp($_REQUEST['appSurname']).".csv";
why can't the file be created? is this even the write snipper to create the file or will it only write to an already created file? or is it simply that i need a folder chmodded so users can write??

thanks in advance.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: How to?? Save web form info to csv....im a bit stuck :)

Post by Jade »

To add a newline you need to use the \n character. That's automatically converted into a paragraph break.

If you're having problems writing to the file try three things:

1) After you open it, chmod it 777

2) If that doesn't work, try chmoding the folder 777

3) If that still doesn't work, make an empty file with the same name before trying to open it. That should always work.
lazynewt
Forum Newbie
Posts: 15
Joined: Mon May 19, 2008 10:43 am

Re: How to?? Save web form info to csv....im a bit stuck :)

Post by lazynewt »

Hi i managed to solve this issue i now have it set up to save to indavidual csv files which are then auto downloaded via ftp every 30mins and imported into a access database. Ill post the code when i get a sec incase anyone else needs it.

thanks for the help anyway jade i appreciate it.
Post Reply