PHP form that generates CSV – commas deletion from the comments field

I have a dual purpose HTML form which return both email and a CSV file to my server.

On my HTML form I have a field titled ‘Notes’ which is a text area. I need to prevent the CSV output file from seeing any commas a user may enter into the text area.

This avoids the ‘Notes’ area of the CSV output file from spanning multiple cells accidentally.

Ideally I would like a code that prevents both commas and returns, but I’ll settle for commas. Please be sure to indicate WHERE your solution should go in my code below. Im newer to PHP and thus don’t understand order.

Here is my PHP code:


    //--------------------------Set these paramaters--------------------------

    // Subject of email sent to you.
    $subject = 'Atlas Race Volunteer Inquiry';  

    // Your email address. This is where the form information will be sent.
    $emailadd = '[email protected]';

    // create email headers

    // Where to redirect after form is processed.
    $url = ''; 

    // Makes all fields required. If set to '1' no field can not be empty. If set to '0' any                or all fields can be empty.
    $req = '0'; 

    // --------------------------Do not edit below this line--------------------------
    $text = "Results from form:nn";
    $space = '  ';
    $line = '

    foreach ($_POST as $key => $value) {
        if ($req == '1'){
            if ($value == ''){
                echo "$key is empty";

        $j = strlen($key);

        if ($j >= 20){
            echo "Name of form element $key cannot be longer than 20 characters";

        $j = 20 - $j;

        for ($i = 1; $i <= $j; $i++){
            $space .= ' ';

        $value = str_replace('n', "$line", $value);
        $conc = "{$key}:$space{$value}$line";
        $text .= $conc;
        $space = '  ';


    mail($emailadd, $subject, $text, 'Reply-To: '.$mail.'', 'From: '.$emailadd.'');

    echo '';

    $filename = "volunteerdata.csv";
    $string = $_POST['Name'].",".$_POST['Email'].",".$_POST['City'].",".$_POST['State'].",".$_POST['Phone'].",".$_POST['ReferredBy'].",".$_POST['Notes']."n";

    if (file_exists($filename)) {
        $file = fopen($filename, "a");
        fwrite($file, $string);
    } else {
        $file = fopen($filename, "a");
        fwrite($file, '"Name","Email","City","State","Phone","ReferredBy","Notes",n');
        fwrite($file, $string);

//example data
$_POST['Email']="[email protected]";
$_POST['ReferredBy']="[email protected]";
$_POST['Notes']="id like to help, but i dont know how";

$foo = array($_POST['Name'],$_POST['Email'],$_POST['City'],$_POST['State'],$_POST['Phone'],$_POST['ReferredBy'],$_POST['Notes']);
$fp = fopen($filename, 'w');
 fputcsv($fp, $foo);

the csv file will look like:

kraysak,[email protected],kraysakland,ky,98756,[email protected],"id like to help, but i dont know how"

this can help you, but, i realize that you don’t know how to do to edit your code, so…

change the

$string = $_POST['Name'].",".$_POST['Email'].","... etc


$string = array($_POST['Name'],$_POST['Email'],$_POST['City'],$_POST['State'],$_POST['Phone'],$_POST['ReferredBy'],$_POST['Notes']);

next, replace the if/else sentece by

if (file_exists($filename)) {
    $file = fopen($filename, 'w');
    fputcsv($file, $string );
} else {
    $file = fopen($filename, 'w');
    fputcsv($file, $string );

i hope this can help you, and in the case, you can make the necessary corrections

Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » PHP form that generates CSV – commas deletion from the comments field

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录