Here is another task where I thinking soo much that, some of my clients do not want to use MySQL database for storing contact form data so they want a single file where all data of contact form stored. So, I think this is an interesting task because we always use MySQL database for storing the data. But today we do Save contact form data in CSV file using PHP.
Live DEMO: Save contact form data in CSV file using PHP
First of all, we create a simple HTML form with some contact-related fields and then using PHP’s POST method for data sending.
You can use a single PHP file for running these complete source code. PHP and HTML form within single PHP File.
How to Run (Save contact form data in CSV file using PHP)
- Create a PHP file named index.php.
- Copy below PHP code and paste on that file.
- Copy below HTML code and paste after PHP code.
- Then run using file name on the browser.
HTML Form
On this simple HTML form, we use the POST method for data sending to PHP variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<!DOCTYPE html> <html> <head> <title>Save contact form data in CSV file using PHP</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /> </head> <body> <br /> <div class="container"> <h2>Save contact form data in CSV file using PHP</h2> <br /> <div class="col-md-6"> <form method="post"> <h3>Contact Form</h3> <br /> <?php echo $error; ?> <div class="form-group"> <label>Enter Name</label> <input type="text" name="name" placeholder="Enter Name" class="form-control" value="<?php echo $name; ?>" /> </div> <div class="form-group"> <label>Enter Email</label> <input type="text" name="email" class="form-control" placeholder="Enter Email" value="<?php echo $email; ?>" /> </div> <div class="form-group"> <label>Enter Subject</label> <input type="text" name="subject" class="form-control" placeholder="Enter Subject" value="<?php echo $subject; ?>" /> </div> <div class="form-group"> <label>Enter Message</label> <textarea name="message" class="form-control" placeholder="Enter Message"><?php echo $message; ?></textarea> </div> <div class="form-group" align="center"> <input type="submit" name="submit" class="btn btn-primary" value="Submit" /> </div> </form> </div> </div> </body> </html> |
Now we checking the form validation using PHP code. Checking if the name field has only letters or not and all other fields like that.
Complete PHP code Saving data on CSV and Validation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
<?php $error = ''; $name = ''; $email = ''; $subject = ''; $message = ''; function clean_text($string) { $string = trim($string); $string = stripslashes($string); $string = htmlspecialchars($string); return $string; } if(isset($_POST["submit"])) { if(empty($_POST["name"])) { $error .= '<p><label class="text-danger">Please Enter your Name</label></p>'; } else { $name = clean_text($_POST["name"]); if(!preg_match("/^[a-zA-Z ]*$/",$name)) { $error .= '<p><label class="text-danger">Only letters and white space allowed</label></p>'; } } if(empty($_POST["email"])) { $error .= '<p><label class="text-danger">Please Enter your Email</label></p>'; } else { $email = clean_text($_POST["email"]); if(!filter_var($email, FILTER_VALIDATE_EMAIL)) { $error .= '<p><label class="text-danger">Invalid email format</label></p>'; } } if(empty($_POST["subject"])) { $error .= '<p><label class="text-danger">Subject is required</label></p>'; } else { $subject = clean_text($_POST["subject"]); } if(empty($_POST["message"])) { $error .= '<p><label class="text-danger">Message is required</label></p>'; } else { $message = clean_text($_POST["message"]); } if($error == '') { $file_open = fopen("contact_data.csv", "a"); $no_rows = count(file("contact_data.csv")); if($no_rows > 1) { $no_rows = ($no_rows - 1) + 1; } $form_data = array( 'sr_no' => $no_rows, 'name' => $name, 'email' => $email, 'subject' => $subject, 'message' => $message ); fputcsv($file_open, $form_data); $error = '<label class="text-success">Thank you for contacting us</label>'; $name = ''; $email = ''; $subject = ''; $message = ''; } } ?> |
Check also:
JavaScript Array | Array length, push, pop, shift, unshift, IndexOf, forEach
Happy coding..!
can you help me to store the selected checkbox value and date of birth into csv file
[…] Save contact form data in CSV file using PHP […]
[…] Save contact form data in CSV file using PHP […]
[…] Save contact form data in CSV file using PHP […]
[…] Save contact form data in CSV file using PHP […]
[…] Save contact form data in CSV file using PHP […]
[…] Save contact form data in CSV file using PHP […]