Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am a little lost. My web page works perfectly fine when i use the default submit button, however, when i use an img the page no longer works correctly. It doesnt seem to be posting the form correctly? code for the form is:
<form action="test.cgi" method="post"> <fieldset> <p><b>User Name:</b> <input type="text" name="username" size="10" maxl +ength="20" value="test" /></p> <p><b>Password:</b> <input type="password" name="password" size="20" m +axlength="20" /></p> <div align="center"><input type="image" name="submit" value="Login" sr +c="blue_login.gif" border=0 /></div> </form>
The form is posted to the same page and i use the following code:
if ($ENV{'REQUEST_METHOD'} eq "GET"){ $my_data = $ENV{'QUERY_STRING'}; } else { $data_length = $ENV{'CONTENT_LENGTH'}; $bytes_read = read(STDIN, $my_data, $data_length); } @name_value_array = split(/&/, $my_data); foreach $name_value_pair (@name_value_array) { ($name, $value) = split(/=/, $name_value_pair); # $name =~ tr/+/ /; $value =~ tr/+/ /; $name =~ s/%(..)/pack("C",hex($1))/eg; $value =~ s/%(..)/pack("C",hex($1))/eg; if($form_data{$name}) { $form_data{$name} .= "\t$value"; } else { $form_data{$name} = $value; } }
Any suggestions would be great!

Replies are listed 'Best First'.
Re: submit button image
by davidrw (Prior) on Jan 16, 2006 at 16:37 UTC
    Without directly addressing the primary issue, switching to the standard CGI might make things a lot easier for you .. See especially the "FETCHING THE PARAMETER LIST AS A HASH:" section of the docs.
    use CGI qw/:standard/; my $q = new CGI; my %form_data = $q->Vars; use Data::Dumper; print Dumper \%form_data;
Re: submit button image
by wfsp (Abbot) on Jan 16, 2006 at 17:03 UTC
    This worked ok for me.
    #!c:/Perl/bin/Perl.exe use strict; use warnings; use CGI::Carp qw(fatalsToBrowser); use Data::Dumper; my ($my_data, $data_length, $bytes_read); if ($ENV{'REQUEST_METHOD'} eq "GET"){ $my_data = $ENV{'QUERY_STRING'}; } else { $data_length = $ENV{'CONTENT_LENGTH'}; $bytes_read = read(STDIN, $my_data, $data_length); } my @name_value_array = split(/&/, $my_data); my %form_data; foreach my $name_value_pair (@name_value_array) { my ($name, $value) = split(/=/, $name_value_pair); $name =~ tr/+/ /; $value =~ tr/+/ /; $name =~ s/%(..)/pack("C",hex($1))/eg; $value =~ s/%(..)/pack("C",hex($1))/eg; if($form_data{$name}){ $form_data{$name} .= "\t$value"; } else{ $form_data{$name} = $value; } } die Dumper \%form_data; __DATA__ Software error: $VAR1 = { 'password' => '', 'submit.y' => '58', 'submit' => 'Login', 'submit.x' => '36', 'username' => 'test' };
    But then again, so did this!
    #!c:/Perl/bin/Perl.exe use strict; use warnings; use CGI::Carp qw(fatalsToBrowser); use CGI; my $q = CGI->new; my $usr = $q->param('username'); my $pwd = $q->param('password'); die "usr: $usr -> pwd: $pwd"; __DATA__ Software error: usr: test -> pwd: at C:/www/local/test2.cgi line 12.
    <html> <head> <title>test.cgi</title> </head> <body> <form action="test2.cgi" method="post"> <fieldset> <p><b>User Name:</b> <input type="text" name="username" size="10" maxlength="20" value="test" /> </p> <p><b>Password:</b> <input type="password" name="password" size="20" maxlength="20" /> </p> <div align="center"> <input type="image" name="submit" value="Login" src="blue_login.gif" border=0 /> </div> </form> </body> </html>
      thanks for those ideas. printing out the data dumper made me aware that the x and y of the image was sent. i didnt allow for this! thanks