Skip to content
Advertisement

PHP form that generates CSV – removing commas 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:

<?php

    //--------------------------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 = 'atlas@atlasrace.com';

    // create email headers

    // Where to redirect after form is processed.  
    $url = 'http://www.atlasrace.com/thankyou4.html'; 

    // 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";
                die;
            }
        }

        $j = strlen($key);

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

        $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 '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';


    $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);
    }

    fclose($file);
?>

Advertisement

Answer

//example data
$_POST['Name']="kraysak";
$_POST['Email']="k@kraysak.gov";
$_POST['City']="kraysakland";
$_POST['State']="ky";
$_POST['Phone']="98756";
$_POST['ReferredBy']="kraysak@k.gov";
$_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);
fclose($fp);

the csv file will look like:

kraysak,k@kraysak.gov,kraysakland,ky,98756,kraysak@k.gov,"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

by

$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'); 
    $head_data=array("Name","Email","City","State","Phone","ReferredBy","Notes");
    fputcsv($file,$head_data);
    fputcsv($file, $string );
}

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

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement