CSV Problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gregwhitworth
Forum Commoner
Posts: 53
Joined: Tue Oct 09, 2007 1:00 am
Location: Wasilla, Alaska

CSV Problems

Post by gregwhitworth »

Okay here is the breakdown of my contact form:
  • Name
    Email
    Phone Number
    Message
    Newsletter
The only required fields are Name, Email, and Message. The key part that I am having problems with is in the newsletter function. When people check that they want the newsletter, I want their email address added to a .csv on my hosting server named newsletter.csv-

It emails perfectly fine, but no CSV anywhere is created or names added to the one that I created. Here is my php Code:

Code: Select all

<?php

if (isset($newsletter)) {
	header("Content-type: application/x-msdownload");
	header("Content-Disposition: attachment; filename=newsletter.csv");
	header("Pragma: no-cache");
	header("Expires: 0");
}

//Contact form field variable settings.
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$msg = $_POST['message'];
$newsletter = $_POST['newsletter'];
$row = 'Email,';
$row .= $email;
$data = $row."\n";

//Configurations for email
$to = "gkwinspired@gmail.com";
$subject = "Form Response at Karen Whitworth.net";
$body = "From: $name <$email>\n\n";
$body .= "$msg";

//newsletter settings


mail($to, $subject, $body, "From: $email");

header ("Location: http://www.karenwhitworth.net/thankyou.htm");


?>
Another problem I am running into, when someone doesn't check that they want the newsletter, I get the following error:
Undefined index: newsletter in D:\users\karenwhitworth.net\templates\scripts\phpForm.php on line 15
I understand that the "newsletter" variable isn't created since it isn't sent, but I don't know how to combat that, maybe (!isset...

Not sure - please help!!

Thanks,
Greg
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

You should initialize the $newsletter variable before to use it in IF:

Code: Select all

<?php

$newsletter = $_POST['newsletter'];

if (isset($newsletter)) {
        header("Content-type: application/x-msdownload");
        header("Content-Disposition: attachment; filename=newsletter.csv");
        header("Pragma: no-cache");
        header("Expires: 0");
}

//Contact form field variable settings.
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$msg = $_POST['message'];

$row = 'Email,';
$row .= $email;
$data = $row."\n";

//Configurations for email
$to = "gkwinspired@gmail.com";
$subject = "Form Response at Karen Whitworth.net";
$body = "From: $name <$email>\n\n";
$body .= "$msg";

//newsletter settings


mail($to, $subject, $body, "From: $email");

header ("Location: http://www.karenwhitworth.net/thankyou.htm");


?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Additional checks are needed to test if the superglobal variables are set.
User avatar
gregwhitworth
Forum Commoner
Posts: 53
Joined: Tue Oct 09, 2007 1:00 am
Location: Wasilla, Alaska

I figured it out....

Post by gregwhitworth »

Thanks for the tips guys, but all I really wanted was fopen, I was making it way too difficult.

--
Greg
Post Reply