Helpful Information
 
 
Category: Server side development
can't write form results to file

Hey people, I'm having trouble here with Perl, CGI. It's an email address only.

The form spits out to the url [email protected]

Once i click on the submit button a error page comes back saying that i have a malformed header..

Heres my CGI code..

#!/usr/local/bin/perl

read(STDIN, $buffer, $ENV{'COTENT_LENGTH'});
@pairs = split (/&/, $buffer);
foreach $pair (@pairs)
{
($name, $value) = split (/=/, $pair);
$email{$name} = $value;
}

$emailFile = "newsletter.txt";

open (FILE, ">>$emailFile");
print FILE "$email{'txtEmail'}";
close (FILE);

i know something is wrong.. im kinda new to this and learning.. Have bought a book called "Perl, CGI, and JavaScript" it's an OK book but it's not helping.. I got this code from the book and it resembles a lot of others codes out there on the web... SO....

Thanks,
Hogtied :D

You get the error "Malformed header" 'cuz there's no header at all. On the web you must send a header, as far as I know.
To send a simple HTML header add
print "Content-type: text/html\n\n";
anywhere to your script and see if that helps.

Mzzl, Chris

well now i don't get the malformed header error but it doesn't write to the file either... do you have any idea why this might be.. The CGI folder does have read/write permissions. And I searched every website that showed you how to write to files. And my code is exactly like theirs with the changed variables of course...

the script looks perfecly fine, perhaps you should try printing some text to the files like this,
print FILE "Try, TRy";
just to see if that part is working fine,

calilo

ive entered the statment:

print FILE "Hello";

and it didn't write anything to the file called newsletter.txt which is in the same CGI folder... I called my web hosting company and they suggested that the code might be the probelm because they don't see any errors at their end.

I have no idea, and as far as i know this should work.... i even tried changing the 'CONTENT_LENGTH' to 'QUERY_STRING' and that didn't work....

Does anyone have any ideas...???

Thanks guys,
Hogtied :confused:

yeh that is deffo a permission problem..i just ran your script on my computer...and its fine....so make sure newsletter.txt is chmod to 777 ..you shouldnt need to chmod its dir but now and again i found i had to on diferent hosts.
sir p

Thanks guys i happened to be a permission error.. Had to create a CGI Zone.. Well any way guys thanks for the help....


Thanks
Hogtied :thumbsup:

ok thought it would be a permission thing...they so anoying hehe.
ok cool
no prob:)
sir p

yeah thanks again sirP. Have another question though, I've have looked everywhere for this code and can't find it. Want, in perl, to set a variable to today's date. I thought it would be something like this

$date = new Date();

but it's not.. See I thought it would be the same as most languages. And I've looked everywhere to see how this would be done but no one explains it. Is there a way to get today's date or do you have to create a variable of today's date with javascript and send it to the CGI??

Thanks,
Hogtied :)

Nevermind I finally found something at www.perl.com/cpan (http://www.perl.com/cpan)

But thanks anyway

Hogtied:p










privacy (GDPR)