Please note, this is a STATIC archive of website technosmarter.com from July 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
 

HTML form data convert into excel file using PHP|CSV file format


The current time you mostly work on MS -Excel (Microsft Excel ) to save and alignment your data in a file.

but you have to work hard for it. If you want to just type text and create it in excel file then it will be so grateful.

. How good would you be to have to fill up a form and just click on a button and generate an Excel file?. You can convert the form data into an excel file (CSV) and It’s very simple or you can import CSV(excel data ) into database import CSV(excel data ) into a database.

Here we will convert HTML form data into CSV file using PHP .

The HTML form is to collect the data from the user side. Here we create the various form fields.
1. Name.

2.Address.

3. Mobile Number.

4. Email Id.

When the user clicks the button the form data will convert into CSV(excel format) File.

Make an HTML form to convert form data into excel file

Now ,we create an HTML form.We create form fileds and a button. When user fills the form and clicks on submit button,the form data converts into excel file.The post method handles the form data. Let's create an HTML form.

HTML Form Code











Create a PHP script code to collect information and convert into CSV(Excel) file

We use PHP script code to handle the form data. The form of data is handled by the PHP post method.
1.First of set form fields on the button using if condition. If the button is pressed, the form data is to be converted into an excel file.

2. Set fields for excel file(CSV format) .

3. User header function and define the content type.

4. At the last fill the form and click on the button. After clicking on the button, you will be got to download a CSV file.

PHP code Here

<?php
if(isset($_POST['submit'])){

    //collect form data
    $name = $_POST['name'];
    $address = $_POST['address'];
    $mobileno = $_POST['mobileno'];
    $email = $_POST['email'];

    //if no errors carry on
    if(!isset($error)){

        // Title of the CSV
              $Content = "Name,Address,Mobile,Emailn";

        //set the data of the CSV
        $Content .= "$name,$address,$mobileno,$emailn";

        //set the file name and create CSV file
        $FileName = $name."-".date("d-m-y-h:i:s").".csv";
        header('Content-Type: application/csv'); 
        header('Content-Disposition: attachment; filename="' . $FileName . '"'); 
        echo $Content;
        exit();
    }
}

//if their are errors display them
if(isset($error)){
    foreach($error as $error){
        echo '

$error

'; } } ?>

Now this time to implement. The final code . Click on Run to execute the script and conver HTML form data into excel file .

Final Code

<?php
if(isset($_POST['submit'])){

    //collect form data
    $name = $_POST['name'];
    $address = $_POST['address'];
    $mobileno = $_POST['mobileno'];
    $email = $_POST['email'];

    //if no errors carry on
    if(!isset($error)){

        // Title of the CSV
              $Content = "Name,Address,Mobile,Email\n";

        //set the data of the CSV
        $Content .= "$name,$address,$mobileno,$email\n";

        //set the file name and create CSV file
        $FileName = $name."-".date("d-m-y-h:i:s").".csv";
        header('Content-Type: application/csv'); 
        header('Content-Disposition: attachment; filename="' . $FileName . '"'); 
        echo $Content;
        exit();
    }
}

//if their are errors display them
if(isset($error)){
    foreach($error as $error){
        echo '

$error

'; } } ?>









<input type="submit" name="submit" value="Get CSV File">
Run

Please Share

Recommended Posts:-

Previous Posts:-

Featured Items:-


Payment form by PayUmoney gateway with GST invoice,email | PHP software

$38



Paypal payment form with email, invoice generator in PHP website | Scripts

$39



Modern blog CMS in PHP with MYSQL database | PHP blog scripts

$67




4 Comments
Guna 13 hours ago

Instead of download csv file after download i need to attached mail is that possible?


Reply

@Guna replied by Techno Smarter 12 hours ago

Yes, this is possible with the PHPMailer library. In the PHPMailer library, you have to provide a download link to your CSV file. 

$mail->addAttachment('path/yourcsvfile.csv');         //Add attachments

Or you can provide specific row data by link .

$mail->addAttachment('path/download.php?id=123');         //Add attachments

Create a download.php file and use the code above and use the id to fetch specific row data to download the CSV file. 

Your path should be complete like - 

$mail->addAttachment('https://yourdomain.com/download.php?id=123');         //Add attachments

By the way , we create unique token for each row to fetch and download CSV file . 

Like - 

$mail->addAttachment('https://yourdomain.com/download.php?token=khjngg5gsansasasaas');         //Add attachments

Instead of id, you have to create a token column in your database. 

How to create unique random id or token key in PHP?

You can find the complete PHPMailer tutorial here 

How to send email using PHP Mailer library and hosting SMTP


Reply

Chelsea Garthwaite 23 Jun

This is the closest thing I have found to what I am looking to do. Instead of creating a new CSV every time there is an entry, how do I change that to a submit button and have one spreadsheet with all entries?


Reply

@Chelsea Garthwaite replied by Techno Smarter 25 Jun

Why don't you use PHP while loop with this code? Create a database and save all your data in your database. Fetch data from the MYSQL database using PHP and use while loop. It will help to create a spreadsheet with all entries.


Reply