How to write txt file on form submit

Project : in this project we will learn how to write txt file on form submit.

index.php
                            
                              <?php include("create-txt.php"); ?>
                                <!DOCTYPE html>
                                <html>
                                <head>
                                 <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
                                </head>
                                <body>
                                  <div class="container "><br>
                                      <div class="row mt-2">
                                          <div class="col-md-6 offset-3 border p-4" style="background: #f5f6fa;"> 
                                            <div class="w-100 text-center"><?php echo $error; ?><?php echo $msg; ?></div>  
                                             <form method="POST" action="index.php" enctype="multipart/form-data">
                                               <h3 class="rounded bg-primary w-100 p-2 text-white text-center">Submit and Write txt File</h3>  
                                              <div class="form-group">
                                                <label for="fullname">Fullname</label>
                                                <input type="text" class="form-control" name="fname" id="fullname">
                                              </div>
                                              <div class="form-group">
                                                <label for="mobile">Mobile</label>
                                                <input type="text" class="form-control" name="mobile" id="mobile">
                                              </div>
                                              <div class="form-group">
                                                <label for="email">Email</label>
                                                <input type="text" class="form-control" name="email" id="email">
                                              </div>
                                              <div class="form-group">
                                                <label for="city">City</label>
                                                <input type="text" class="form-control" name="city" id="city">
                                              </div>
                                              <div class="form-group">
                                                <label for="country">Country</label>
                                                <input type="text" class="form-control" name="country" id="country">
                                              </div>
                                              <div class="form-group">
                                                <button type="submit" name="submit" class="btn btn-success">Submit and Write txt File</button>
                                              </div>
                                          </form>
                                         </div>
                                      </div>         
                                  </div>
                                </body>
                                </html>
                            
                          

Submit Form

Form User Interface : insert data in database and save as txt file.

index.php
PHP

Database and table name

You have to create database and table in phpMyAdmin.

check image

PHP
Create table in databse
Table in database
                              
                                -- Table structure for table `datas`
                                CREATE TABLE `datas` (
                                  `id` int(11) NOT NULL AUTO_INCREMENT,
                                  `fullname` varchar(100) NOT NULL,
                                  `mobile` varchar(20) NOT NULL,
                                  `email` varchar(100) NOT NULL,
                                  `city` varchar(100) NOT NULL,
                                  `country` varchar(100) NOT NULL
                                );
                              
                          

Create database connection

Create connection with database
myproject/dbcon.php
dbcon.php
    
        $dbhost ="localhost";
        $dbuser ="root";
        $dbpswd ="";
        $dbname ="dbdemo";
        $dbcon= mysqli_connect($dbhost,$dbuser,$dbpswd,$dbname);
        if($dbcon){
            //echo "Connected............!!!";
            return $dbcon;
            exit();
        }else{
            die("Connection failed ? ? ?". mysqli_error($dbcon));
        }
        mysqli_close($dbcon);
    

Insert data in database & Save as txt File

Process to insert data and save as txt file.

create-txt.php
                              
                              <?php  
                              include('dbcon.php');
                              $error=''; $msg='';
                              if(isset($_POST['submit'])){
                                $fname= ucwords($_POST['fname']);
                                $mobile= $_POST['mobile'];
                                $email= $_POST['email'];
                                $city= ucfirst($_POST['city']);
                                $country= ucfirst($_POST['country']);
                                  if(empty($fname) || empty($mobile) || empty($email)||empty($city)||empty($country)){
                                    $error = "<div class='alert alert-danger text-danger'>All the fields are required ?</div>";
                                  }else{
                                    $sql="INSERT INTO datas(fullname, mobile,email,city,country) VALUES('$fname','$mobile','$email','$city','$country')";
                                    $run = mysqli_query($dbcon,$sql);
                                    if($run){
                                      $file_open = fopen('user-data.txt','a');
                                        $save_data = [
                                          'fullname'=> ucwords($fname),
                                          'mobile'  => $mobile,
                                          'email'   => $email,
                                          'city'    => ucwords($city),
                                          'country' => ucwords($country),
                                        ];
                                        foreach ($save_data as  $data) {
                                          fwrite($file_open, $data.', ');
                                        }
                                      $msg = "<div class='alert alert-success text-success'>The data has been submitted </div>";
                                      $fname =''; $mobile =''; $email =''; $city=''; $country='';
                                      fclose($file_open);
                                    }else{
                                      $error = "<div class='alert alert-danger text-danger'>The data is not submitted and save</div>";
                                    }
                                  }
                              }
                              mysqli_close($dbcon);
                            
                          

Saved TXT File

Check project folder the txt file will be created user-data.txt filename

PHP

css folder

You must download and include bootstrap css source files for good interface OR You can add cdn link from bootstrap website.
myproject/css
bootstrap.min.css