Getting form data into CSV file with PHP

Put this file together and then placed it up on my Linux server:

PHP Array Practice
<?=$message;?>
First Name Last Name
Address
City State ZIP
Phone Email Please send me email
Comments
<?php $fn = $_POST['fn']; $ln = $_POST['ln']; $address = $_POST['address']; $city = $_POST['city']; $state = $_POST['state']; $zip = $_POST['zip']; $phone = $_POST['phone']; $email = $_POST['email']; $emailMe = (isset($_POST['emailMe'])) ? $_POST['emailMe'] : 'No'; $comments = $_POST['comments']; //validate if(empty($fn) || empty($ln) || empty($address) || empty($city) || empty($state) || empty($zip) || empty($phone) || empty($email)){//show the form $message = 'Fill in areas in red!'; $aClass = 'errorClass'; //this is where the creating of the csv takes place $cvsData = $fn . "," . $ln . "," . $address . "," . $city . "," . $state . "," . $zip . "," . $phone . "," . $email . "," .$emailMe . "," . $comments ."\n"; //then we open the file: $fp = fopen("raTest.csv","a"); // $fp is now the file pointer to file $filename //And then we write the form contents to the file: if($fp){ fwrite($fp,$cvsData); // Write information to the file fclose($fp); // Close the file ?>

I have created the raTest.csv file with header lines and set permissions on everything wide open. If I pull out the <PHP?, the HTML form displays correctly but all the PHP comes out as plain text. I got this code off the HUBS site so it is pretty old. Maybe my version of PHP is off?

I am trying to fill in a Risk Assessment form a little at a time and dump the contents into a CSV file. From there it will go into LibreOffice Calc and some math will need to be performed. The final CSV will have about 35 rows and 70 columns but I want to chunk it up. The plan is to use checkboxes for the columns to allow the user to “check” all vulnerabilities that apply to each row and put them into the CSV file.

There are a few issues with your script:[br]

[ul][li]* Make sure that your filename ends ‘.php’ so that the server knows ito interpret it’s contents appropriately. If the php elements of your script are showing in the webpage you’ve probably used the suffix ‘.html’.[/li]
[li]* All PHP code within your page needs to be enclosed within ‘<?php' and '?>’.[/li]
[li]* Make sure you use ‘echo’ to output anything any variable within the HTML e.g. <?php echo $somevariable; ?> you can’t use ‘=’ in this context.[/li][/ul]

I have amended your code so that it works but I would recommend comparing it to the code you posted to identify how and where it has been changed.[br][br]Working code below:
[php]html>

PHP Array Practice
<?php echo $message;?>
First Name Last Name
Address
City State ZIP
Phone Email Please send me email
Comments
<?php $sub = $_POST['Submit']; if ($sub) { $fn = $_POST['fn']; $ln = $_POST['ln']; $address = $_POST['address']; $city = $_POST['city']; $state = $_POST['state']; $zip = $_POST['zip']; $phone = $_POST['phone']; $email = $_POST['email']; if(isset($_POST['emailMe'])&&($_POST['emailMe'] == 'Yes')){ $emailme = "Yes"; } else { $emailme = "No"; } $comments = $_POST['comments']; //validate if(empty($fn) || empty($ln) || empty($address) || empty($city) || empty($state) || empty($zip) || empty($phone) || empty($email)) { $message = "Fill in areas in red!"; $aClass = "errorClass"; } else { //this is where the creating of the csv takes place $cvsData = $fn . "," . $ln . "," . $address . "," . $city . "," . $state . "," . $zip . "," . $phone . "," . $email . "," .$emailme . "," . $comments ."\n"; //then we open the file: $fp = fopen("raTest.csv","a"); // $fp is now the file pointer to file $filename //And then we write the form contents to the file: if($fp){ fwrite($fp,$cvsData); // Write information to the file fclose($fp); // Close the file } $message = "Thanks for submitting your information!"; } echo $message; } ?> [/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service