2010-04-23 76 views
1

我想要一个可以做两件事的cgi脚本。如何在CGI.pm下包含框架集?

  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; 

回答

1

如果要改变用户的浏览器显示的页面的结构(即,当创建一个表单提交了以前不存在的框架),你将不得不使用客户端边javascript来做到这一点。这可能与框架存在但可见并在提交表单时显示它一样简单,或者可能涉及DOM操作来实际创建它。

不过,根据您的具体要求,您最好使用空白< div>代替框架并通过AJAX填充; CGI::Ajax将是最简单的方法来做到这一点。由于您无论如何需要使用JavaScript(以显示或创建框架),所以基于AJAX的方法不会为您的网站用户添加任何新的要求。

编辑:哇...驱动器downvotes的任何解释?你认为我没有回答这个问题吗?我真的错了吗?开导我!

+0

谢谢,@ DJTripleThreat,但这不是关于点(我已经足够了)。我只是想知道如果我错了。 – 2010-04-28 09:47:01

+0

http://meta.stackexchange.com/questions/22934/so-annoyed-with-no-comment-vindictive-downvoting – 2010-04-29 14:23:21

3

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

阅读上如何框架集工作:

1

提问者说:

我也想在生成结果后,才帧存在/打印。

这使得它很棘手,尽管CGI.pm确实支持帧。你没有引用你为什么要使用框架集,所以你必须决定使用框架集方法是否真的值得遇到麻烦。

您需要使用条件和补充信息来控制输出内容:何时打印查询表格,何时打印框架集以及何时打印各个框架。诀窍是在期望的时间输出框架集,其中带有pathinfo的脚本指向后面的帧以指示要输出的HTML /帧。

首先,看看关于CGI。这里帧时支持:

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

我试图修改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"; 
} 
2

提问者说:

我也想了帧结果产生/印刷后,才存在。

这使得它很棘手,尽管CGI.pm确实支持帧。你没有引用你为什么要使用框架集,所以你必须决定使用框架集方法是否真的值得遇到麻烦。

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

您可能需要看到:

  1. 首先,查看有关帧CGI.pm支持这里
    文件 - 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%,*">` 
    

    b。然后使用JavaScript动态调整帧大小。使用从http://stein.cshl.org/WWW/CGI/examples/frameset.txt代码作为一个例子,你就必须修改print_response日常输出的JavaScript修改的框架来调整帧(即暴露你的隐藏响应帧):

    parent.document.getElementsByTagName("FRAMESET").item(1).cols = '10,90';