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

一応、「get」での受信をやってみた。受け取り後の内容解析は、前回の時にやってしまったので、今回はARGVの受信部分を少し変更しただけ。

メソッドが「get」の時は、環境変数の「QUERY_STRING」にフォームの内容が入る。リンクでコマンドを送ると「ARGV」だけでなく「QUERY_STRING」にも入ってしまうので、ちょっと扱いに困るけど・・・。

とりあえず、この例では「ARGV」をまず見て、それが空の場合だけ動作するようにした。次に「QUERY_STRING」が空なら送信フォームを、そうじゃなければ、その内容を分解して表示するようにした。

ソースコード

  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
#!/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 = 'テスト18';
$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){
        if($ENV{'QUERY_STRING'}){
            &amp;formRead;
            &amp;formWrite;
        }else{
            &amp;printForm("get");
        }
    }else{
        &amp;error('<h2>無効なコマンドが渡されました。</h2>');
    }

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

    exit;
}

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($_)); }
}
Hugo で構築されています。
テーマ StackJimmy によって設計されています。