PDA

View Full Version : Form To File Scripting


Revolver Ocelot
11-17-2005, 11:22 PM
Hello,
I wonder if you guys could help me. I'm really sorry, I've got virtually no knowledge of programming, and no money to work with.
I've been searching high and low all over the internet just to find a simple form processing script to save three separate form inputs to three separate files.
Basically, all I want to do is to be able to update (by replacement) three text files by filling in the details in a form. Surely that's not unheard of? I've found a lot of form-to-mail scripts, but none that save the information to a server-side text file.

Can anybody point me in the right direction?:confused:
Thanks very much!

windumi
12-07-2005, 07:37 AM
perl could easily help you to do it



use GUI;
$cgi=new CGI;

$a=$cgi->param("firstfield");
$b=$cgi->param("secondfield");
$c=$cgi->param("thirdfield");

open(FILE1,"filename1.txt");
print FILE1,$a;
open(FILE2,"filename2.txt");
print FILE2,$b;
open(FILE3,"filename3.txt");
print FILE3,$c;

close FILE1;
close FILE2;
close FILE3;

FishMonger
12-07-2005, 08:03 AM
You should always check the return value of opening the filehandles and if you need to open the files in write mode not read.
#!/usr/bin/perl -w

use strict;
use CGI qw(:standard);


open(FILE1,">filename1.txt") || die "can't open filename1.txt $!";
print FILE1 param("firstfield");
close FILE1;

open(FILE2,">filename2.txt") || die "can't open filename2.txt $!";
print FILE2 param("secondfield");
close FILE2;

open(FILE3,">filename3.txt") || die "can't open filename3.txt $!";
print FILE3 param("thirdfield");
close FILE3;

_PANKAJ_
12-08-2005, 06:21 PM
FishMonger ... that Script Isnt Workin....

can u help me with this ?...

the html form which .. the user puts the name n comments .. n it shud be saved in a txt file .....

i placed the txt file in my cgi bin folder ....:rolleyes:

#!/usr/bin/perl -w

use strict;
use CGI qw(:standard);

$name = param('name');
$comments = param('comments');


open(FILE,">test.txt") || die "can't open test.txt $!";
print FILE param("name");
print FILE param("comments");
close FILE;

FishMonger
12-08-2005, 07:14 PM
What part "Isnt Workin...."?
Have you looked at the web server error log?
What error message(s) are you getting?

See if adding some debug code helps
#!/usr/bin/perl -w

use strict;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

print header();
warningsToBrowser(1);

print start_html();

open(FILE,">test.txt") || die "can't open test.txt $!";
print FILE param("name");
print FILE param("comments");
close FILE;

print "Form info saved";
print end_html();

_PANKAJ_
12-08-2005, 07:56 PM
yep Thanks Fish Monger .. Its Workin Now :thumbsup:

_PANKAJ_
12-08-2005, 08:10 PM
FishMonger its workin but the problem is ...

if the second person enters the name n comments....

the 1st persons name n comments r overwritten...

i mean it doesnt goes on addin names a comment .... its addin 1 comment...
if second person enter comments it deletes the 1st comment n overwrite it...

can u tell ... how it'll keep on addin info ... without erasing the 1st one :confused:

FishMonger
12-08-2005, 08:44 PM
Currently, the file is being opened in write mode, which means that it over writes the file each time it's opened. You need to open it in append mode and you may also want to and a linebreak to each print statement.

open(FILE,">> test.txt") die "can't open test.txt $!";

print FILE param("name") . "\n";
print FILE param("comments") . "\n\n";
close FILE;

_PANKAJ_
12-08-2005, 09:11 PM
Edit : ok i got it thnx alot