フォームからの入力(その2)

いろいろ試してみた結果、やはりフォームからでは、メソッドが「get」の時は「ARGV」が使えない。「action」に設定しても、フォームの内容に上書きされてしまうのか、無視されるようだ。

サーバのログを見ると、リンクやイメージでの呼出しは「get」で発生している。おなじ「get」でも、この場合はちゃんと「ARGV」に入ってくれる。フォームで「get」からコマンド的な物を使うには、「type=hidden」で渡すしかないようだ。それはそれで興味深いけど。

ともかく、そういうわけで、この例では「get」では送信できません。っていうか、受信できないのか・・・。送信後に「post」か「get」かを選ぶ画面に戻ってしまいます。

一つのCGIスクリプトで色々な動作ができるというのは、なかなか面白い。癖になってしまう。

そういえば、エラーがあるとき、DOSプロンプトから直に実行すると、ちゃんとエラーの内容も出てくれる。エラーがあるときにブラウザから実行すると、「無効なまたは認識されない応答をサーバーが返しました」とか「ドキュメントにデータが含まれていません」とかいうエラーしかでない。これはなかなか貴重だ。チョット面倒だけど・・・。

ソースコード

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/perl

# 初期設定
# require Jcode;
# $JcodeVer = "Jcode $Jcode::VERSION";
# *Jgetcode = &Jcode::getcode;
# *Jconvert = &Jcode::convert;

require "../../../cgi-bin/jcode.pl";
$JcodeVer = "jcode.pl $jcode::version";
*Jgetcode = &jcode::getcode;
*Jconvert = sub { &jcode::to($_[1], $_[0], $_[2]); };

@G_styles = ('../../ipp.css','../test.css');
$G_title = 'テスト17';
$G_myCode = &Jgetcode('漢字');
$G_Code = 'jis';
$G_Charset = 'iso-2022-jp';
%G_form=();

$G_scrName = $ENV{'SCRIPT_NAME'};
if($G_scrName =~ /ts[0-9]{4}/){
    $G_scrName = $&;
    $G_linkFile = "../$G_scrName.htm";
}

{
    &printHeader;
    print "<div class=test>n";

    if($#ARGV == -1){
        &amp;printMethodtype;
    }elsif($ARGV[0] eq "output"){
        &amp;formRead;
        &amp;formWrite;
    }elsif($ARGV[0] eq "post"){
        &amp;printForm("post");
    }elsif($ARGV[0] eq "get"){
        &amp;printForm("get");
    }else{
        &amp;error('<h2>無効なコマンドが渡されました。</h2>');
    }

    print "</div>n";
    &amp;printFooter;

    exit;
}

sub printMethodtype{
    &amp;Jprint("<form method=post action="$ENV{'SCRIPT_NAME'}?post"><input type=submit value="POSTでの送信フォーム"></form>n");
    &amp;Jprint("<form method=post action="$ENV{'SCRIPT_NAME'}?get"><input type=submit value="GETでの送信フォーム"></form>n");
}

sub error{
    &amp;Jprint(@_);
    print "</div>n";
    &amp;printFooter;
    exit;
}

sub formWrite{
    local($name, $value);
    print "<table border=1>n";
    &amp;Jprint("<tr><th>エレメントの名前<th>エレメントの中身</tr>n");
    while (($name, $value) = each(%G_form)){
        &amp;JconvPrint("<tr><td>$name<td>$value</tr>n");
    }
    print "</table>n";
}

sub formRead{
    local($query_string, @elements, $elm, $name, $value, $code, $method);
    $method = $ENV{'REQUEST_METHOD'};
    if($method eq "POST"){
        read(STDIN, $query_string, $ENV{'CONTENT_LENGTH'});
    }elsif($method eq "GET"){
        $query_string = $ENV{'QUERY_STRING'};
    }else{
        &amp;error('methodは「post」か「get」にしてください。');
    }
    @elements = split(/&amp;/, $query_string);  # 中身はpostもgetも同じ
    foreach $elm (@elements){
        ($name, $value) = split(/=/, $elm);
        $value =~ s/+/ /g; # =~ tr/+/ /; と書くのが普通なのか?
        $value =~ s/%([0-9a-fA-F][0-9a-fA-F])/pack("C", hex($1))/eg;
        $code = &amp;Jgetcode($value);
        if($code ne 'euc'){ $value = &amp;Jconvert($value, 'euc', $code); } # とりあえずEUCに変換
        $value =~ s/&amp;/&amp;amp;/g;  # メタ文字(?)を無効化
        $value =~ s/</&amp;lt;/g;   # タグを無効化
        $value =~ s/>/&amp;gt;/g;   # タグを無効化
        $value =~ s/rn?/n/g; # 改行を統一
        $value =~ s/n/<br>/g;  # 改行を<br>に変換
        $G_form{$name} = $value;
    }
}

sub printForm{
    print "<form method=$_[0] action="$ENV{'SCRIPT_NAME'}?output">n";
    print "<input type=text name=tx1 value="tx1"><br>n";
    print "<input type=text name=tx2 value="tx2"><br>n";
    &amp;Jprint("<textarea name=tx3 cols=40 rows=4>tx3 日本語 英語 どちらでもnタグなどは無効になります。</textarea><br>n");
    print "<input type=submit value="Submit!">n";
    print "</form>n";
}

sub printHeader{
    if($G_Charset){
        print "Content-type: text/html; charset=$G_Charsetnn";
    }else{
        print "Content-type: text/htmlnn";
    }
    print '<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">'."n";
    print "<html lang=ja>n<head>n";
    if($G_Charset){ print "<meta http-equiv="Content-Type" content="text/html; charset=$G_Charset">n"; }
    print '<meta http-equiv="Content-Script-Type" content="text/javascript">',"n";
    print '<meta http-equiv="Content-Style-Type" content="text/css">'."n";
    &amp;Jprint ("<title>$G_title</title>n");
    foreach (@G_styles){ print "<link rel="stylesheet" type="text/css" href="$_">n"; }
    print "</head>n<body>n";
    print "<div class=head>n";
    &amp;Jprint ("<h1>$G_title</h1><hr>n");
    &amp;printlinks;
    print "<hr></div>n";
}

sub printFooter{
    print "<div class=foot><hr>n";
    &amp;printlinks;
    print "<hr>n";
    &amp;Jprint("漢字コード変換 : $JcodeVer<br>n");
    open(IN, '../../sig.txt');
    print while (<IN>);
    close(IN);
    print "</div>n";
    print "</body></html>n";
}

sub printlinks{
    print "<a href="../../../index.htm">Home</a>n";
    print "/n<a href="../../">Perl</a>n";
    print "/n<a href="../">TestCGI Index</a>n";
    if($G_linkFile){ &amp;Jprint ("/n<a href="$G_linkFile">$G_titleの解説</a>n"); }
}

sub Jprint{
    if($G_Code eq $G_myCode){
        foreach (@_){ print; }
    }else{
        foreach (@_){ print &amp;Jconvert($_, $G_Code, $G_myCode); }
    }
}

sub JconvPrint{
    foreach (@_){ print &amp;Jconvert($_, $G_Code, &amp;Jgetcode($_)); }
}
comments powered by Disqus
Hugo で構築されています。
テーマ StackJimmy によって設計されています。