Home>

Currently, we want to output the information entered in the web page form to CSV.
#Form item example
・ Name (text box)
・ Address (text box)
・ Telephone number (text area)

#The input form is as follows. (Excerpt)

<form method = "post" action = "mail.php">
<table>
<tr>
<th>name</th>
<td><input size = "50" type = "text" name = "name" /></td>
</tr>
  <tr>
  <th>address</th>
    <td><input size = "50" type = "text" name = "address" /></td>
</tr>
  <tr>
<th>phone number</th>
    <td><input size = "50" type = "text" name = "phone number" /></td>
  </tr>

# mail.php (partial excerpt)

<? php
   // assign the obtained value to a variable
   $name = $_POST ['your name'];
   $address = $_POST ['address'];
   $tel = $_POST ['phone number'];
   // Preparing data to write to CSV file
   // $data = 'name, address, phone number';// column title
     $data. = "\ n";
     $data. = $name;
     $data. = ',';
     $data. = $address;
     $data. = ',';
     data. = $tel;
     $data. = "\ n";
   }
   $file = fopen ('./ csv/form.csv', 'a');
   fputs ($file, $data);
   fclose ($file);
?>

The code above seems to be able to output other than the date and time
I would like to output the following data as "form.csv".

-------------------------------------------- ----------------------------
Date and time | Name | Address | Phone number
-------------------------------------------------- ----------------------
2018/08/31 11:00 | Last Name | Kyoto XXX | 090-XXXX-XXXX
-------------------------------------------------- ----------------------
2018/09/01 9:00 | Last name | Osaka Prefecture XXX | 080-XXXX-XXXX
-------------------------------------------------- ----------------------


Thanks for your cooperation.

  • Answer # 1

    Does this mean?

    <? php
       // assign the obtained value to a variable
       $name = $_POST ['your name'];
       $address = $_POST ['address'];
       $tel = $_POST ['phone number'];
       // Preparing data to write to CSV file
       // $data = 'name, address, phone number';// column title
         $data. = "\ n";
         $data. = (new DateTime ())->format ('Y/m/d i: h');
         $data. = ',';
         $data. = $name;
         $data. = ',';
         $data. = $address;
         $data. = ',';
         data. = $tel;
         $data. = "\ n";
       }
       $file = fopen ('./ csv/form.csv', 'a');
       fputs ($file, $data);
       fclose ($file);