PHP form inserts data into CSV file: "PHP form inserts data into CSV file
84
rate or flag this page
By Alpho011
Reference Materials
Excel 2007 For Dummies (For Dummies (Computer/Tech)) Excel 2007 For Dummies (For Dummies (Computer/Tech))
Price: $11.98
List Price: $21.99
Microsoft Office Excel 2007 Microsoft Office Excel 2007
Price: $174.95
List Price: $229.95
Microsoft Office Excel 2007 Version Upgrade Microsoft Office Excel 2007 Version Upgrade
Price: $85.49
List Price: $109.95
Build Your Own Database Driven Website Using PHP and MySQL Build Your Own Database Driven Website Using PHP and MySQL
Price: $7.00
List Price: $39.95
Today we are going to use a .csv (comma seperated values) file to store values from a online PHP web form.
A csv file is file that you can create easily with Microsoft Excel, here are the full and upgrade versions, also you can also get knowledge from this recommended book on excel for further knowledge that is beyond the scope of this tutorial.
Ok 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 do this when you can use a database:
1. Data is portable
2. Data is readily readable by MS office
3. Data is web ready
4. Web hosting is simple, FTP and done.
Before we start the files can be found here.
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'><span>First Name</span></td>
<td width='215'><input class='<?=$aClass;?>' type='text' name='fn' id='fn' /></td>
<td width='62'><span>Last 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>
<td><span>City</span></td>
<td><input class='<?=$aClass;?>' type='text' name='city' id='city' /></td>
<td><span>State</span></td>
<td width='148'><input class='<?=$aClass;?>' type='text' name='state' id='state' /></td>
<td width='24'><span>ZIP</span></td>
<td width='255'><input class='<?=$aClass;?>' type='text' name='zip' id='zip' /></td>
</tr>
<tr>
<td><span>Phone</span></td>
<td><input class='<?=$aClass;?>' type='text' name='phone' id='phone' /></td>
<td><span>Email</span></td>
<td><input class='<?=$aClass;?>' type='text' name='email' id='email' /></td>
<td><input name='emailMe' type='checkbox' id='emailMe' value='Yes' checked='checked' /></td>
<td>Please send me email</td>
</tr>
<tr>
<td colspan='6'><span>Comments
<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 want, even then one from my other article.
Then we need to create a csv file;
Excel and for this particular one we created the following headers:
First Name
Last Name
Address
City
State
ZIP
Phone
Email
Yes/No
Comments
Those will go across the first row and will match our 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, somebody may have miss 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
And 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 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 conjuction with email, storing in 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."
วันพุธที่ 30 กันยายน พ.ศ. 2552
สมัครสมาชิก:
ส่งความคิดเห็น (Atom)
ไม่มีความคิดเห็น:
แสดงความคิดเห็น