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,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

'; } } ?>









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

Recommended Posts:-

Previous Posts:-

Shop Items:-


Integrate Payumoney payment gateway in PHP website | Source Scripts

$19



Dynamic Countdown timer using JS ,PHP and MYSQL | Animated timer

$10



Payment form using PayUmoney payment Gateway in PHP | Source Scripts

$19






Need your help to reach 1000 subscribers . Please subscribe Now.We are just near .

”Subscribe

free instagram followers free instagram likes