Vous êtes sur la page 1sur 6

PHP form inserts data into CSV file

Today we are going to use a .csv (comma seperated values) file to store values from an online PHP
web form.
A .csv file is a file that you can create easily with Microsoft Excel (further knowledge is beyond the
scope of this tutorial).
Okay, first off we will use the multi-purpose page technique from Build Database Driven Website
Using PHP and MySql.
We aren't using a database, we are going to use Excel in .csv form to store the form data.
Why http://adorablewinter120.shutterfly.com/adorablewinter120 do this when you can use a
database?
Data is portable
Data is readily readable by MS office
Data is web ready
Web hosting is simple, FTP and done.
Since my server location is offline, email me for the src code, sorry.

First we create a simple form:


form id="form1" name="form1" method="post" action="?=$_SERVER['PHP_SELF'];?"
table class="formatTblClass"
tr
th colspan="6"?=$message;?/th
/tr
tr
td width="68"spanFirst Name/span/td
td width="215"input class="?=$aClass;?" type="text" name="fn" id="fn" //td
td width="62"spanLast Name/span/td
td colspan="3"input class="?=$aClass;?" name="ln" type="text" id="ln" size="50" //td

/tr
tr
td colspan="6"table width="100%" border="0" cellspacing="0" cellpadding="0"
tr
td width="71"Address/td
td width="721"input class="?=$aClass;?" name="address" type="text" id="address" size="100" //td
/tr
/table/td
/tr
tr
tdspanCity/span/td
tdinput class="?=$aClass;?" type="text" name="city" id="city" //td
tdspanState/span/td
td width="148"input class="?=$aClass;?" type="text" name="state" id="state" //td
td width="24"spanZIP/span/td
td width="255"input class="?=$aClass;?" type="text" name="zip" id="zip" //td
/tr
tr
tdspanPhone/span/td
tdinput class="?=$aClass;?" type="text" name="phone" id="phone" //td
tdspanEmail/span/td
tdinput class="?=$aClass;?" type="text" name="email" id="email" //td
tdinput name="emailMe" type="checkbox" id="emailMe" value="Yes" checked="checked" //td
tdPlease send me email/td
/tr
tr

td colspan="6"spanComments
textarea name="comments" id="comments" cols="45" rows="5"/textarea
/span
div align="center"
input type="submit" name="Submit" id="Submit" value="Submit" /
input type="reset" name="Reset" id="button" value="Reset" /
/div/td
/tr
/table
/form
Same deal, simple form, great results, you can use this technique using any type of form you
http://www.cityofkearney.org/jobs.aspx want, even one from my other articles.

Then we need to create a .csv file.


For this particular one we created the following headers:
First Name
Last Name
Address
City
State
ZIP

Phone
Email
Yes/No
Comments

These headers will go across the first row and will match the variables in our PHP script to insert
them into the sheet.

After clicking the submit button, we want to do some checks:


$fn = $_POST['fn'];
$ln = $_POST['ln'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$emailMe = (isset($_POST['emailMe'])) ? $_POST['emailMe'] : 'No';
$comments = $_POST['comments'];
//validate

if(empty($fn) || empty($ln) || empty($address) || empty($city) || empty($state) || empty($zip) ||


empty($phone) || empty($email)){//show the form

$message = 'Fill in areas in red!';


$aClass = 'errorClass';
In this case, we show the form again, in case somebody may have missed some things we find
important. If all is good, we get to the good stuff, the insert:
First, we tie all the data up in a variable called $csvData:
//this is where the creating of the csv takes place
$cvsData = $fn . "," . $ln . "," . $address . "," . $city . "," . $state . "," . $zip . "," . $phone . "," . $email
. "," .$emailMe . "," . $comments ."\n";
then, we open the file:
$fp = fopen("formTest.csv","a"); // $fp is now the file pointer to file $filename
then, we write the form contents to the file:
if($fp){
fwrite($fp,$cvsData); // Write information to the file
fclose($fp); // Close the file
And finally, close the connection or file.
Simple. Once again look over the source files and get a feel for doing this. These techniques can be
used in conjunction http://kearney.net/ with email, storing in a db, and storing this in a regular text
file. The limit is your imagination.
Please be sure to leave any questions or comments you may have about this, and enjoy your projects!

http://alpho011.hubpages.com/hub/PHP-form-inserts-data-into-CSV-file

Vous aimerez peut-être aussi