How can i write the values of a form through a cgi script in a txt file.

J

JR

How can i write the values of a form through a cgi script in a txt file.

My html pages is for example:

<FORM METHOD=POST ACTION="script.cgi">
<input type="text" name="nummer" size="20"></p>
<p><input type="text" name="paswoord" size="20"></p>
<p><input type="submit" value="Submit" name="Send">
</form>

Can anybody help me with this simple script
I am a newbie and i would love to learn perl/cgi.


Thanks
 
A

Anand

In your cgi script names 'script.cgi' you can get values of parameter
passed. You have to write values to file in this script.
Let me know if you need some more input.

--Anand
 
J

JR

Tore Aursand said:
#!/usr/bin/perl

Should be:

#!/usr/bin/perl
#
use strict;
use warnings;
print "Content-type:text/html\n\n";

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}

Should be:

use CGI;

my $cgi = CGI->new();
print $cgi->header(-type => 'text/html');
open (EEP,"passwords.txt");foreach $key (keys(%FORM)) {
print EEP "$key = $FORM{$key}<br>";
}

Should be:

my $params = $cgi->Vars();
open( EEP, '>passwords.txt' ) || die "$!";
foreach my $key ( keys %$params ) {
print EEP $key . ' = ' . $params{$key} . '<br';
}
close( EEP );

But why do you want to write some HTML to STDOUT, while the form data gets
written to a text file?

Summary:

perldoc CGI
perldoc -f open

I tried your code but i get the follow error

Status: 302 Found Location:
/bin/error?error=Your%20script%20produced%20this%20error%3A%20%3Cbr%3EUndefined%20subroutine%20CGI%3A%3AVars%20at%20CGI.pm%20line%20349.
URI: /bin/error?error=Your%20script%20produced%20this%20error%3A%20%3Cbr%3EUndefined%20subroutine%20CGI%3A%3AVars%20at%20CGI.pm%20line%20349.
Content-type: text/html
 
J

JR

Tore Aursand said:
Please post the complete code.

#!/usr/bin/perl
#
use strict;
use warnings;

use CGI;
my $cgi = CGI->new();
print $cgi->header(-type => 'text/html');

my $params = $cgi->Vars();
open( EEP, '>passwords.txt' ) || die "$!";
foreach my $key ( keys %$params ) {
print EEP $key . ' = ' . $params{$key} . '<br';
}
close( EEP );

this is my complete code

Thanks

Jo
 
E

Eric Bohlman

(e-mail address removed) (JR) wrote in
foreach my $key ( keys %$params ) {

Here you treat $params as the hash reference it is...
print EEP $key . ' = ' . $params{$key} . '<br';

But here you're not dereferencing it, instead you're trying to look up the
key in the non-existent hash %params. You want $params->{$key}, or
$$params{$key} if you prefer (though I find the arrow-style easier to
read).
 
T

Tore Aursand

[...]
this is my complete code

That's impossible. You told in the previous message that you got this
error message:

Undefined subroutine CGI::Vars at CGI.pm line 349

When I run your code - as posted by you - I don't receive this error
message. I get, however, an other error:

Global symbol "%params" requires explicit package name [...]

And that's because you're not dereferencing your hash when printing it out;

print EEP $key . ' = ' . $params{$key} . '<br';
^^^^^^^^^^^^^
You should use '$params->{$key}' instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top