If you want to save ‘Form’ data or any other data to ‘Excel/CSV’, you are on the right blog.
Here we will save Form Data to a ‘CSV’ File instead of any Database, we are going to use PHP for this.
(We are assuming you have knowledge of PHP)
So let’s get started.
STEP 1:
Create a CSV File ‘contact_data.csv‘. (Open Ms Excel and save a file in CSV Format)
Write the column name in the first row of the ‘contact_data.csv’.
(Here we are writing “sr_no”,”name”,”email”,”subject”,”message” )
STEP 2:
Create a simple HTML Form index.html with action ‘post.php’, Code given below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <form action="post.php" method="post"> <h3 align="center">Contact Form</h3> <br /> <div class="form-group"> <label>Enter Name</label> <input type="text" name="name" placeholder="Enter Name" class="form-control" value="" /> </div> <div class="form-group"> <label>Enter Email</label> <input type="text" name="email" class="form-control" placeholder="Enter Email" value="" /> </div> <div class="form-group"> <label>Enter Subject</label> <input type="text" name="subject" class="form-control" placeholder="Enter Subject" value="" /> </div> <div class="form-group"> <label>Enter Message</label> <textarea name="message" class="form-control" placeholder="Enter Message"></textarea> </div> <div class="form-group" align="center"> <input type="submit" name="submit" class="btn btn-info" value="Submit" /> </div> </form> |
STEP 3:
Create Action File ‘post.php‘, Code given below.
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 | <?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 = [ "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 = ""; } } echo $error; ?> |