Thursday, 14 September 2017

How to Store Form data in CSV File using PHP



This is new post on PHP web development tutorial and this We have describe topic like How to store HTML Form data in CSV file by using PHP script. We all knwo CSV is a comma-seperated value file in which it stores tabular data in plain text format. It store one record in one line and for second record it will store in second line. We can also view this file data in spreadsheet format also and we can easily export this file data in excel also. This type of file mostly used for heavy data exchange.

So, In this post we have make simple script for store contact form data in CSV file, So when some has come to our website and he want to send feedback to us then he can fill form and submit. When he submit form then that form data will be stored in CSV file which we have define in script and stored on our working folder or even you can also store on web server also. For make this type of system here we have use PHP script. We have use some PHP in build function like fopen() for open file for write operation, file() function for get file data in array format for count number of rows in file and fputcsv() function for write form data in csv file. So by using this in build PHP function we have developed simple system for storing form data in CSV file.

Here we have store data CSV file because it is widely used in web development and it can be open in notepad also. It is light weight that means it occupy less space than other spreadsheet type like excel. So it is mostly used for data exchange. For example suppose in your web site your start one campaign and you want to received feedback from user and that feedback data you don't want to store in your website database, so at that time you can use this feature and you can store data in simple csv file and this data you can load on webpage also as per your requirement. In short in this post we have discuss topic like how to store form data in CSV file by using PHP script.







Source Code



<?php
//index.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 = '';
 }
}

?>
<!DOCTYPE html>
<html>
 <head>
  <title>How to Store Form data in CSV File using PHP</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
 </head>
 <body>
  <br />
  <div class="container">
   <h2 align="center">How to Store Form data in CSV File using PHP</h2>
   <br />
   <div class="col-md-6" style="margin:0 auto; float:none;">
    <form method="post">
     <h3 align="center">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-info" value="Submit" />
     </div>
    </form>
   </div>
  </div>
 </body>
</html>

25 comments:

  1. thanks for providing source code sir

    ReplyDelete
  2. sir where is the location of csv file & where to put it in our database or php coding.

    ReplyDelete
  3. I got it THanks in anticipation.

    ReplyDelete
  4. I have copied the code and tried to emulate this but the csv file isn't getting updated. Both the files are in the same folder and I'm using apache2 server and php7. Please help me with this!

    ReplyDelete
  5. Thanks for sharing this..The code is working perfectly..I need to add headers for this csv, How..?

    ReplyDelete
  6. how to curd csv file using php without sql

    ReplyDelete
  7. oh Man finally i have got the code i was fucked up with mysql database 3 days i'm trying codes even when i copy it from videos it always comes with an error !

    Thanks man You are awesome !

    ReplyDelete
  8. how to search data in csv file

    ReplyDelete
  9. Is there any way to also add a fileupload?

    ReplyDelete
  10. There are many errors I get on the output page

    ReplyDelete
  11. I want some help
    pls provide me your email id

    ReplyDelete
  12. thanku so much sir ji and your great job

    ReplyDelete
  13. form works and it creates csv file as it should, but it would be nice if message achieved recipient :)

    ReplyDelete
  14. There are a lot of errors I don't know where the problem lies.

    ReplyDelete
  15. How to send data to email as well including csv export??

    ReplyDelete
  16. i am getting a error on line number 20

    ReplyDelete
  17. Brilliant - Thank you kindly

    ReplyDelete
  18. man.....it works . just put name of colum on top of each colum

    ReplyDelete
  19. is it all code in 1 file or 2 different files ?

    ReplyDelete
  20. This code is fantabulous !!!!!
    Not a single error.
    As far as I can see, all the code is in 1 file.
    The CSV file was created.
    Exactly as I entered it in the form.
    WOW !!!!!

    ReplyDelete