Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7694577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:21:06+00:00 2026-05-31T21:21:06+00:00

I have been trying to make it work with either fwrite or fputcsv but

  • 0

I have been trying to make it work with either fwrite or fputcsv but i always get the errors pointing to parameters. I am newbie in php but I try to experiment and see what it does. but so far it doesn’t either work because of parameters or not writing to the file (nothing on csv file).

I have one big problem. How to have text appear in columns (name, email and comments) while writing all data into three columns?

$csvData = array($name,$email,$comment);
        foreach($csvData as $key => $val)
        {
            $strVal = $val;

        }
        print $strVal;
        $fileHandle="";
        $filename='I-have-question.csv';
        if(!file_exists($filename)){
            $fileHandle = fopen($filename, "a+");
        }

        fwrite($fileHandle, $strVal);
        fclose($fileHandle);

insight/guide would be appreciated. thanks

EDIT

$name=$_POST['contactName'];
        $email=$_POST['email'];
        $comment=$_POST['comment'];


        $csvData = array($name,$email,$comment);
        $csvCol = array ('name', 'email', 'comment');

        $strVal='';
        foreach($csvData as $key => $val)
        {
            $strVal .= $val;

        }
        print $strVal;
        $fileHandle="";
        $filename='I-have-question.csv';

            $fileHandle = fopen($filename, "a+");


        fputcsv($filehandle,$csvData);
        fclose($fileHandle);

Now error says: Warning: fputcsv() expects parameter 1 to be resource, null. I changed according to two inputs. But it doesnt go well.

EDIT #2

Thank you, @Baba and @Quasdunk for the inputs. The only question left is how to bring in the array for $csvCol to be appended to the first row for each column so that the csvData can be appended/written after that. Only once for csvcol. how do I go about it?

EDIT #3
Thank you very much for the help. I investigated on downloading csv from the browser after the user click on the link – is it possible to do that in that same code? or I hardcode the link to the csv? Just wonder. I saw somewhere about downloadable csv where I click submit button and then the file pop up to be saved – thats not what I want but I only need the csv file to be downloaded from the server via the link.

EDIT #4

Thank you, @Baba and @Quasdunk for the inputs again.

 if(isset($_POST[$interest]))
    { 
      foreach ($interest as $interests)
       {
            $interest = $interests . " ,";
       }
    }

then in csv, it showed “array”, not values of checkboxes, i.e. 1, 2,3,4

$csvData = array($name,$email,$comment,array($interest));

Where have I gone wrong with this?

EDIT #5

Thank you very much, @Baba. I have last question
Do you know by chance how to pass the variable to mail()? For example, after csv is made/written, then mail() should send all the data to user/admin.

So far I got it working but the interest still doesn’t show the selected checkboxes – instead print “Array” similar to csv.

$interest=$_POST[‘interests’];

this is above the forceheader function.

$emailTo = get_option('tz_email');
        if (!isset($emailTo) || ($emailTo == '') ){
            $emailTo = get_option('admin_email');
        }
        $subject = 'I Have A Question to Ask from '.$name;
        $thanksubject = 'Thank you for the Form Submission';
        $body = "Name: $name \n\nEmail: $email \n\nInterest $interest \n\nComments: $comments";
        $userbody = "";
        $headers = 'From: '.$name.' ' . "\r\n" . 'Reply-To: ' . $emailTo;
        //$headers .= 'Bcc: email2@example.com' . "\r\n";
        //$headers .= 'Bcc: email3@example.com' . "\r\n";

        wp_mail($emailTo, $subject, $body, $headers);
        wp_mail($email,$thanksubject, $body, $headers);
        $emailSent = true;

This is after writing to csv. it would be sent if there is no error such as blank fields. $interest only register one array, for example if I select all checkboxes, it would show all in email. So far I tested and it only show one last checkbox. Do you know where I went wrong?

UPDATED

I have figured it out and managed to write the checkboxes’ values to csv and emails as well.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:21:07+00:00Added an answer on May 31, 2026 at 9:21 pm

    This would work , its accepts array and converts it to CSV automatically

        $csvData = array($name,$email,$comment);
        $filename='I-have-question.csv';
        $isNew = (file_exists($filename) || is_file($filename)) ? false : true ;
        touch($filename); //Sets access and modification time of file and  If the file does not exist, Create it.
        $fp = fopen($filename, 'w');
        if($isNew) //Add header Information if its new 
        {
            fputcsv($fp , array ('NAME', 'EMAIL', 'COMMENT'));
        }
        fputcsv($fp, $csvData);
        fclose($fp);
    
    
        //To Force CSV downlaod  
        header("Content-type: application/csv");
        header("Content-Disposition: attachment; filename=file.csv");
        header("Pragma: no-cache");
        header("Expires: 0");
        readfile($filename);
    

    A new concept to always force header information even if it does not exist before

            $csvData = array($name,$email,$comment);
            $filename='I-have-question.csv';
            $isNew = (file_exists($filename) || is_file($filename)) ? false : true ;
            touch($filename); //Sets access and modification time of file and  If the file does not exist, Create it.
            $fp = fopen($filename, 'a');
            forceHeader(array ('NAME', 'EMAIL', 'COMMENT'),$filename);
            fputcsv($fp, $csvData);
            fclose($fp);
    
    
    
    
            function forceHeader(Array $headerParam,$csvFile)
            {
                $headerParam = implode(",", $headerParam);
                $fileHeader = file_get_contents($csvFile, NULL, NULL, 0, strlen($headerParam));
                if($headerParam != $fileHeader)
                {
                    $content = $headerParam ."\r\n" ;
                    $content .= file_get_contents($csvFile, NULL, NULL, strlen($headerParam) + 2);
                    file_put_contents($csvFile,$content);
                }
    
            }
    

    ==== Interest =====

    $interest = isset($_POST[$interest]) ? implode("; " , $_POST[$interest]) : array();
    $csvData = array($name,$email,$comment,$interest);
    

    Thanks
    🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to make a case for using Python at my work.
I have been trying to get yui-css grid system to make a three column
I have been trying to make something work with JQuery. I have the following
I have been trying to make this work for a long time now. In
I have been trying to make a div fade in evey 30sec and out
I have been trying to make the sub-menu horizontal. In my HTML it looks
I have been trying to make an init script using start-stop-daemon. I am stuck
I have been trying to make this to be a little jQuery plugin that
I have been trying to make a Cross-platform 2D Online Game, and my maps
I have been trying to test my application to make sure that all the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.