1

我想要一个做两件事的 cgi 脚本。

  1. 从表单中获取输入。
  2. 根据帧上的输入值生成结果。

我还希望框架在生成/打印结果后才存在。

下面是我想要做的简化代码。但不知何故,它不起作用。正确的方法是什么?

    #!/usr/local/bin/perl

    use CGI ':standard';

    print header;
    print start_html('A Simple Example'),
        h1('A Simple Example'),
        start_form,
        "What's your name? ",textfield('name'),
        p,
        "What's the combination?",
        p,
        checkbox_group(-name=>'words',
               -values=>['eenie','meenie','minie','moe'],
               -defaults=>['eenie','minie']),
        p,
        "What's your favorite color? ",
        popup_menu(-name=>'color',
               -values=>['red','green','blue','chartreuse']),
        p,
        submit,
        end_form,
        hr;

    if (param()) {

        # begin create the frame

print <<EOF;
<html><head><title>$TITLE</title></head>
<frameset rows="10,90">
<frame src="$script_name/query" name="query">
<frame src="$script_name/response" name="response">
</frameset>
EOF

# Finish creating frame


        print 
        "Your name is: ",em(param('name')),
        p,
        "The keywords are: ",em(join(", ",param('words'))),
        p,
        "Your favorite color is: ",em(param('color')),
        hr;
    }
    print end_html;
4

4 回答 4

3

HTML 框架集引用其他文档。您不会一次性创建它们并在一个响应中将它们全部发送给用户代理。仅打印框架集和框架引用,浏览器将做额外的工作来单独获取每个框架。

阅读框架集的工作原理:

于 2010-04-23T15:33:30.913 回答
2

提问者 说:

我还希望框架仅在生成/打印结果后才存在。

尽管 CGI.pm 确实支持帧,但这让它变得很棘手。您没有引用为什么要使用框架集,因此您必须决定使用框架集方法是否真的值得麻烦。

一种选择是用隐藏的框架来伪造它。

您可能需要查看:

  1. 首先,在此处查看 CGI.pm 对帧的支持
    Doc - http://stein.cshl.org/WWW/CGI/#frames
    示例 - http://stein.cshl.org/WWW/CGI/examples/frameset.txt
    试试 - http://stein.cshl.org/WWW/CGI/examples/frameset.pm

    有效地使用框架可能很棘手。要创建并排显示查询和响应的适当框架集,您需要将脚本分为三个功能部分。第一部分应该创建声明并退出。第二部分负责创建查询表单并将其引导到一个框架中。第三部分负责创建响应并将其引导到不同的框架中。

  2. 请参阅此参考以动态修改框架集:

    一个参考 - http://www.codeguru.com/forum/archive/index.php/t-373259.html
    其他参考 - http://www.google.com/search?q=javascript+dynamically+resize+frameset +cols

    一个。因此,首先您将首先创建框架集,但隐藏响应框架:

    <frameset rows="100%,*">`  
    

    湾。然后稍后使用 javascript 动态调整框架的大小。以http://stein.cshl.org/WWW/CGI/examples/frameset.txt中的代码为例,您必须修改print_response例程以输出 javascript 以修改框架集以调整框架大小(即公开您的隐藏响应框架):

    parent.document.getElementsByTagName("FRAMESET").item(1).cols = '10,90';
    
于 2010-05-02T16:49:03.953 回答
1

提问者 说:

我还希望框架仅在生成/打印结果后才存在。

尽管 CGI.pm 确实支持帧,但这让它变得很棘手。您没有引用为什么要使用框架集,因此您必须决定使用框架集方法是否真的值得麻烦。

您需要使用条件和补充信息来控制何时输出:何时打印查询表单、何时打印框架集以及何时打印单个框架。诀窍是在所需时间输出框架集,框架指向脚本后面,路径信息指示要输出哪个 HTML/框架。

首先,在此处查看 CGI.pm 对帧的支持:

有效地使用框架可能很棘手。要创建并排显示查询和响应的适当框架集,您需要将脚本分为三个功能部分。第一部分应该创建声明并退出。第二部分负责创建查询表单并将其引导到一个框架中。第三部分负责创建响应并将其引导到不同的框架中。

我试图修改http://stein.cshl.org/WWW/CGI/examples/frameset.txt来尝试做你想做的事,但我没有/不能测试它(没有 CGI.pm 服务器一应俱全)。我严重怀疑它会在没有一些调试的情况下工作。但希望这可以为您提供使用它的基本思路。首先研究http://stein.cshl.org/WWW/CGI/examples/frameset.txt然后在下面查看我的更改:

#!/usr/local/bin/perl

### UNTESTED CODE ###

use CGI;
$query = new CGI;
print $query->header;
$TITLE="Frameset Example";

# We use the path information to distinguish between calls
# to the script to:
# (1) create the frameset
# (2) create the query form
# (3) create the query response

$path_info = $query->path_info;

# If no path information is provided, then we create 
# print query form            ###new####
# a side-by-side frame set    ###old###
if (!$path_info) {
    #&print_frameset;   ###old### 
    &print_html_header; ###new###
    &print_query        ###new###
    &print_end;         ###new###
    exit 0;
}

# If response path             ###new###
if ($path_info=~/response/) {  ###new###
    &print_frameset;           ###new###
    exit 0;                    ###new###
}                              ###new###

# If we get here, then we either create the query form
# or we create the response.
&print_html_header;
#&print_query if $path_info=~/query/;             ###old###
#&print_response if $path_info=~/response/;       ###old###
&print_query if $path_info=~/frame-query/;        ###new###
&print_response if $path_info=~/frame-response/;  ###new###
&print_end;


# Create the frameset
sub print_frameset {
    $script_name = $query->script_name;
    print <<EOF;
<html><head><title>$TITLE</title></head>
<frameset cols="50,50">
<!--frame src="$script_name/query" name="query"-->           <!--###old###-->
<!--frame src="$script_name/response" name="response"-->     <!--###old###-->
<frame src="$script_name/query" name="frame-query">          <!--###new###-->
<frame src="$script_name/response" name="frame-response">    <!--###new###-->
</frameset>
EOF
    ;
    exit 0;
}

sub print_html_header {
    print $query->start_html($TITLE);
}

sub print_end {
    print qq{<P><hr><A HREF="../index.html" TARGET="_top">More Examples</A>};
    print $query->end_html;
}

sub print_query {
    $script_name = $query->script_name;
    print "<H1>Frameset Query</H1>\n";
    #print $query->startform(-action=>"$script_name/response",-TARGET=>"response");   ###old###
    print $query->startform(-action=>"$script_name/response");  ###new###
    print "What's your name? ",$query->textfield('name');
    print "<P>What's the combination?<P>",
    $query->checkbox_group(-name=>'words',
          -values=>['eenie','meenie','minie','moe']);

    print "<P>What's your favorite color? ",
    $query->popup_menu(-name=>'color',
         -values=>['red','green','blue','chartreuse']),
    "<P>";
    print $query->submit;
    print $query->endform;
}

sub print_response {
    print "<H1>Frameset Result</H1>\n";
    unless ($query->param) {
 print "<b>No query submitted yet.</b>";
 return;
    }
    print "Your name is <EM>",$query->param(name),"</EM>\n";
    print "<P>The keywords are: <EM>",join(", ",$query->param(words)),"</EM>\n";
    print "<P>Your favorite color is <EM>",$query->param(color),"</EM>\n";
}
于 2010-05-02T16:48:12.973 回答
1

如果您想更改用户浏览器中显示的页面结构(即,在提交以前不存在的表单时创建一个框架),您将不得不使用客户端 javascript 来执行此操作。这可以简单到让框架存在但可见并在提交表单时显示它,或者它可能涉及 DOM 操作来实际创建它。

但是,根据您的具体要求,您最好使用空的 <div> 而不是框架并通过 AJAX 填充它;CGI::Ajax将是执行此操作的最简单方法。由于您无论如何都需要使用 javascript(显示或创建框架),因此基于 AJAX 的方法不会为您的站点用户添加任何新要求。

编辑:哇......对开车投票的任何解释?你认为我没有回答这个问题吗?我完全错了吗?开导我!

于 2010-04-23T09:16:09.527 回答