问题描述:

I have been trying to run a simple perl-cgi script on windows XP. This is a simple HTML form with an Submit button where clicking on Submit button displays some text(Username). But clicking the Submit button on the HTML page, nothing is happening. If I open up browser with url its working fine.

HTML Form:

<form id="form" name="form" method="post" action="C:/Server/Apache2/cgi-bin/hello.cgi" enctype="multipart/form-data">

<h1><center>User Login</center></h1>

<p><label><h4>Username</h4></label>

<input type="text" name="username" id="username" /></p>

<p><label><h4>Password</h4></label>

<input type="password" name="password" id="password" /></p>

<button type="submit">Sign-In</button><br><br>

CGI :

 #!C:\perl\bin\perl.exe -wT

local ($buffer, @pairs, $pair, $name, $value, %FORM);

# Read in text

$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;

if ($ENV{'REQUEST_METHOD'} eq "POST")

{

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

}else {

$buffer = $ENV{'QUERY_STRING'};

}

# Split information into name/value pairs

@pairs = split(/&/, $buffer);

foreach $pair (@pairs)

{

($name, $value) = split(/=/, $pair);

$value =~ tr/+/ /;

$value =~ s/%(..)/pack("C", hex($1))/eg;

$FORM{$name} = $value;

}

$user_name = $FORM{username};

print "Content-type:text/html\r\n\r\n";

print "<html>";

print "<head>";

print "<title>Hello - Second CGI Program</title>";

print "</head>";

print "<body>";

print "<h2>Hello $user_name - Second CGI Program</h2>";

print "</body>";

print "</html>";

1;

Opening CGI directly in browser:

网友答案:

You need proper URL in form action attribute, ie. action="/cgi-bin/hello.cgi"

网友答案:

Another suggestion just to add.

Since it looks like you are just starting to learn perl, I would read into the CGI module. CGI is a widely used perl module for programming CGI : Common Gateway Interface which is used to recieve user input and produce HTML output. It processes form submissions, query string manipulation, and processing and preparing HTTP headers.

There are two styles of programming with CGI, object-oriented and function-oriented.

相关阅读:
Top