【问题标题】:HTML form action to execute cgi is not working执行 cgi 的 HTML 表单操作不起作用
【发布时间】:2013-05-20 06:53:18
【问题描述】:

我一直试图在 Windows XP 上运行一个简单的 perl-cgi 脚本。这是一个带有提交按钮的简单 HTML 表单,单击提交按钮会显示一些文本(用户名)。但是单击 HTML 页面上的提交按钮,什么也没有发生。如果我用 url 打开浏览器,它工作正常。

HTML 表单:

<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;

直接在浏览器中打开 CGI:

【问题讨论】:

  • apache 对错误日志说了什么?
  • @choroba:日志中没有错误。但我发现了另外一件事。如果我在我的 html 表单中使用 'action="/cgi-bin/hello.cgi"',因为我的 httpd.conf 有别名 ScriptAlias /cgi-bin/ "C:/Server/Apache2/cgi-bin/",通过单击提交按钮打开一个带有 url C:\cgi-bin\hello.cgi 的页面。但应该是C:/Server/Apache2/cgi-bin/hello.cgi

标签: perl apache cgi


【解决方案1】:

您需要action 属性形式的正确URL,即。 action="/cgi-bin/hello.cgi"

【讨论】:

  • 如果我通过单击提交按钮在我的 html 中使用 action="/cgi-bin/hello.cgi",则会打开一个带有 url C:\cgi-bin\hello.cgi 的页面
  • 你的 html 表单的 url 是什么?
  • 我将 action 更改为 action=http://localhost/cgi-bin/hello.cgi 并开始工作。如果我想使用任何.pl 文件,例如action=http://localhost/cgi-bin/hello.pl,我应该将我的httpd.conf 编辑为AddHandler cgi-script .pl 吗??
  • 那是明智的,但在表单操作中使用绝对 url 并不多。如果您从另一台计算机访问 Web 表单会怎样?
  • action="/cgi-bin/hello.cgi" 你没有告诉我你的 html 表单的 url 是什么
【解决方案2】:

另一个建议只是添加。

由于您似乎刚刚开始学习 perl,因此我会阅读 CGI 模块。 CGI 是一个广泛使用的 perl 模块,用于编程CGI : Common Gateway Interface,用于接收user input 并产生HTML 输出。它处理表单提交、查询字符串操作以及处理和准备 HTTP 标头。

使用 CGI 进行编程有两种风格,object-orientedfunction-oriented

【讨论】:

    猜你喜欢
    • 2013-09-19
    • 2016-11-15
    • 1970-01-01
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多