duanchuang1935 2018-01-28 20:19
浏览 97

PHP - 将结果表单转换为CSV - 在写入之前替换逗号

I am trying to change / strip / replace the COMMAS from the results of a form before it is saved to a CSV file.

The basic process:

  1. form filled out by a user. submit
  2. result written to a CSV file (open, write, close)
  3. email to an email address. (mail: to, subject, message, headers)

I have tried the wrapping of data with "\" as described here >> Write to .csv file with PHP (Commas in Data Error)

This is my basic form > form.php

<form action="thanks.php" method="post" >
    <label for="Title">Title</label>
    <select name="Title" class="form-control">
        <option value=""> - select - </option>
        <option value="Mr.">Mr.</option>
        <option value="Mrs.">Mrs.</option>
    </select>

    <label for="FirstName">First Name *</label>
    <input name="FirstName" type="text" value="" id="FirstName">

    <label for="LastName"> Last Name *</label>
    <input name="LastName" type="text" value="" id="LastName">

    <label for="Email">Email Address *</label>
    <input name="Email" type="text" value="" id="Email">

    <label for="Comments">Comments</label>
    <textarea name="Comments" rows="2" class="form-control" id="Comments"></textarea>

    <label for="Description">Description</label>
    <textarea name="Description" rows="2" class="form-control" id="Description"></textarea>

    <button type="submit">Submit</button>
</form>

What I have got working:

  1. Collecting of filled out form data to thanks.php
  2. Opening and writing to a csv file (well twice - ones a backup)
  3. Sending of forms data as html to an email

thanks.php

    <?php extract ($_POST); ?>
    <?php
        $Title =        $_POST['Title'];
        $FirstName =    $_POST['FirstName'];
        $LastName =     $_POST['LastName'];
        $Email =        $_POST['Email'];
        $Description =  $_POST['Description'];
        $Comments =     $_POST['Comments'];

        $fp = fopen("thedata.csv", "a");
        $savestring =
            $arr[] = "\"".$Title."\",".
            $arr[] = "\"".$FirstName."\",".
            $arr[] = "\"".$LastName."\",".
            $arr[] = "\"".$Email."\",".
            $arr[] = "\"".$Description."\",".
            $arr[] = "\"".$Comments."\"".
            "
";
        fwrite($fp, $savestring);
        fclose($fp);

        $fp = fopen("backupdata.csv", "a");
        $savestring =
            $arr[] = "\"".$Title."\",".
            $arr[] = "\"".$FirstName."\",".
            $arr[] = "\"".$LastName."\",".
            $arr[] = "\"".$Email."\",".
            $arr[] = "\"".$Description."\",".
            $arr[] = "\"".$Comments."\"".
            "
";
        fwrite($fp, $savestring);
        fclose($fp);
    ?> 

    <?php
    {
      PRINT "<h1>Thanks for filling out the form</h1>";
    }
    $to  = "email@address.com".",";
    $subject = "Contact the client";
    $message = "
      <html><head><style>body,p,td{font-family:calibri; font-size:11pt; color: #000000}</style></head><body>
        <table cellpadding=5>
          <tr>
            <td>Title</td>
            <td>FirstName</td>
            <td>LastName</td>
            <td>Email</td>
            <td>Description</td>
            <td>Comments</td>
          </tr>
          <tr>
            <td>$Title</td>
            <td>$FirstName</td>
            <td>$LastName</td>
            <td>$Email</td>
            <td>$Description</td>
            <td>$Comments</td>
          </tr>
        </table>
         </body>
      </html>
    ";
    $headers  = "MIME-Version: 1.0" . "
";
    $headers .= "Content-type: text/html; charset=iso-8859-1" . "
";
    $headers .= "From:email@address.com" . "
"; 
    mail($to, $subject, $message, $headers);
    ?>

What is NOT working...

When I open the csv file (in excel/sheets), I noticed that people are adding commas to the description and / or comments fields... so there are extra columns automatically.

I have tried using semi-colons instead in $savestring but then it doesn't even separate into columns. Just one line, and where I put a comma in description that's where the next column comes.

So I know I need to replace the commas THEY type in with hyphens (or something else) BEFORE I write to CSV so that columns are NOT created, therefore breaking the formatting of data into extra columns.

I just can't figure out how :|

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题