2010-04-23 38 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';