blog/content/post/2009/06/03/2009-06-03-00001170.md

121 lines
12 KiB
Markdown

---
title: フォームを用いたデータの受け渡し
author: kazu634
date: 2009-06-03
wordtwit_post_info:
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4635;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- Perl
---
<div class="section">
<p>
<a href="http://d.hatena.ne.jp/asin/4774134392" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/asin/4774134392', 'これだけは知っておきたい Webアプリケーションの常識');">これだけは知っておきたい Webアプリケーションの常識</a>』を読んで、サンプルを打ち込んでいます。仕事でウェブアプリのインフラ周りに関わりそうなので、勉強です。でも、本当はTomcatとかやらないといけないんだよな。。。
</p>
<h4>
html側
</h4>
<p>
Ubuntuだと、/var/www/配下(Apacheで公開するように設定しているディレクトリ)に適当な名前で保存すればいい。
</p>
<pre class="syntax-highlight">
<span class="synComment">&#60;!DOCTYPE html PUBLIC &#34;-//W3C//DTD XHTML 1.0 Transitional//EN&#34; &#34;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#34;&#62;</span>
<span class="synIdentifier">&#60;</span><span class="synStatement">html</span><span class="synIdentifier"> mlns=</span><span class="synConstant">&#34;http://www.w3.org/1999/xhtml&#34;</span><span class="synIdentifier"> </span><span class="synType">lang</span><span class="synIdentifier">=</span><span class="synConstant">&#34;ja&#34;</span><span class="synIdentifier"> xml:</span><span class="synType">lang</span><span class="synIdentifier">=</span><span class="synConstant">&#34;ja&#34;</span><span class="synIdentifier">&#62;</span>
<span class="synIdentifier">&#60;</span><span class="synStatement">head</span><span class="synIdentifier">&#62;</span>
<span class="synPreProc"> </span><span class="synIdentifier">&#60;</span>metattp-equiv<span class="synIdentifier">=</span><span class="synConstant">&#34;Content-Type&#34;</span><span class="synIdentifier"> </span><span class="synType">content</span><span class="synIdentifier">=</span><span class="synConstant">&#34;text/html; charset=UTF-8&#34;</span><span class="synIdentifier"> /&#62;</span>
<span class="synPreProc"> </span><span class="synIdentifier">&#60;</span><span class="synStatement">title</span><span class="synIdentifier">&#62;</span>フォームの例<span class="synIdentifier">&#60;/</span><span class="synStatement">title</span><span class="synIdentifier">&#62;</span>
<span class="synPreProc"> </span><span class="synIdentifier">&#60;/</span><span class="synStatement">head</span><span class="synIdentifier">&#62;</span>
<span class="synIdentifier">&#60;</span><span class="synStatement">body</span><span class="synIdentifier">&#62;</span>
<span class="synIdentifier">&#60;</span><span class="synStatement">form</span><span class="synIdentifier"> </span><span class="synType">name</span><span class="synIdentifier">=</span><span class="synConstant">&#34;ym&#34;</span><span class="synIdentifier"> </span><span class="synType">method</span><span class="synIdentifier">=</span><span class="synConstant">&#34;post&#34;</span><span class="synIdentifier"> </span><span class="synType">action</span><span class="synIdentifier">=</span><span class="synConstant">&#34;cgi-bin/weekday.pl&#34;</span><span class="synIdentifier">&#62;</span>
<span class="synIdentifier">&#60;</span><span class="synStatement">p</span><span class="synIdentifier">&#62;</span>
Year: <span class="synIdentifier">&#60;</span><span class="synStatement">input</span><span class="synIdentifier"> </span><span class="synType">type</span><span class="synIdentifier">=</span><span class="synConstant">&#34;text&#34;</span><span class="synIdentifier"> </span><span class="synType">name</span><span class="synIdentifier">=</span><span class="synConstant">&#34;year&#34;</span><span class="synIdentifier"> </span><span class="synType">size</span><span class="synIdentifier">=</span><span class="synConstant">&#34;10&#34;</span><span class="synIdentifier"> /&#62;</span> <span class="synIdentifier">&#60;</span><span class="synStatement">br</span><span class="synIdentifier"> /&#62;</span>
Month: <span class="synIdentifier">&#60;</span><span class="synStatement">input</span><span class="synIdentifier"> </span><span class="synType">type</span><span class="synIdentifier">=</span><span class="synConstant">&#34;text&#34;</span><span class="synIdentifier"> </span><span class="synType">name</span><span class="synIdentifier">=</span><span class="synConstant">&#34;month&#34;</span><span class="synIdentifier"> </span><span class="synType">size</span><span class="synIdentifier">=</span><span class="synConstant">&#34;10&#34;</span><span class="synIdentifier"> /&#62;</span>
<span class="synIdentifier">&#60;</span><span class="synStatement">input</span><span class="synIdentifier"> </span><span class="synType">type</span><span class="synIdentifier">=</span><span class="synConstant">&#34;submit&#34;</span><span class="synIdentifier"> </span><span class="synType">name</span><span class="synIdentifier">=</span><span class="synConstant">&#34;submit&#34;</span><span class="synIdentifier"> </span><span class="synType">value</span><span class="synIdentifier">=</span><span class="synConstant">&#34;送信&#34;</span><span class="synIdentifier"> /&#62;</span>
<span class="synIdentifier">&#60;/</span><span class="synStatement">p</span><span class="synIdentifier">&#62;</span>
<span class="synIdentifier">&#60;/</span><span class="synStatement">form</span><span class="synIdentifier">&#62;</span>
<span class="synIdentifier">&#60;</span><span class="synStatement">address</span><span class="synIdentifier">&#62;</span>
<span class="synIdentifier">&#60;</span><span class="synStatement">a</span><span class="synIdentifier"> </span><span class="synType">href</span><span class="synIdentifier">=</span><span class="synConstant">&#34;mailto:simoom634@kazu634.local&#34;</span><span class="synIdentifier">&#62;</span><span class="synUnderlined">simoom634</span><span class="synIdentifier">&#60;/</span><span class="synStatement">a</span><span class="synIdentifier">&#62;</span>
<span class="synIdentifier">&#60;/</span><span class="synStatement">address</span><span class="synIdentifier">&#62;</span>
<span class="synIdentifier">&#60;/</span><span class="synStatement">body</span><span class="synIdentifier">&#62;</span>
<span class="synIdentifier">&#60;/</span><span class="synStatement">html</span><span class="synIdentifier">&#62;</span>
</pre>
<h4>
cgi側
</h4>
<p>
Ubuntuだと、/usr/bin/cgi-bin/配下に「weekday.pl」として下のスクリプトを保存する:
</p>
<pre class="syntax-highlight">
<span class="synPreProc">#!/usr/bin/perl</span>
<span class="synComment"># === Libraries ===</span>
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synStatement">use </span>CGI;
<span class="synStatement">use </span>Time::Local;
<span class="synComment"># === Main part ===</span>
<span class="synStatement">my</span> <span class="synIdentifier">$q</span> = <span class="synStatement">new</span> CGI;
<span class="synComment"># クエリから年と月を得る</span>
<span class="synStatement">my</span> <span class="synIdentifier">$year</span> = <span class="synIdentifier">$q</span>-&#62;param(<span class="synConstant">'year'</span>);
<span class="synStatement">my</span> <span class="synIdentifier">$month</span> = <span class="synIdentifier">$q</span>-&#62;param(<span class="synConstant">'month'</span>);
<span class="synComment"># 指定された月の1日を得る</span>
<span class="synStatement">my</span> <span class="synIdentifier">$firstday</span> = timelocal(<span class="synConstant"></span>, <span class="synConstant"></span>, <span class="synConstant"></span>, <span class="synConstant">1</span>, <span class="synIdentifier">$month</span> - <span class="synConstant">1</span>, <span class="synIdentifier">$year</span> - <span class="synConstant">1900</span>);
<span class="synComment"># その年の曜日を得る</span>
<span class="synStatement">my</span> (<span class="synIdentifier">$n_sec</span>, <span class="synIdentifier">$n_min</span>, <span class="synIdentifier">$n_hour</span>, <span class="synIdentifier">$n_day</span>, <span class="synIdentifier">$n_mon</span>, <span class="synIdentifier">$n_year</span>, <span class="synIdentifier">$w_day</span>) = <span class="synStatement">localtime</span>(<span class="synIdentifier">$firstday</span>);
<span class="synStatement">my</span> <span class="synIdentifier">$wdaystr</span> = (<span class="synConstant">'Sun'</span>, <span class="synConstant">'Mon'</span>, <span class="synConstant">'Tue'</span>, <span class="synConstant">'Wed'</span>, <span class="synConstant">'Thu'</span>, <span class="synConstant">'Fri'</span>, <span class="synConstant">'Sat'</span>)[<span class="synIdentifier">$w_day</span>];
<span class="synStatement">print</span>(<span class="synConstant">&#34;Content-type: text/html; char-set=utf-8</span><span class="synSpecial">\n\n</span><span class="synConstant">&#34;</span>);
<span class="synStatement">print</span>(<span class="synConstant">&#34;</span><span class="synIdentifier">$year年$month月は$wdaystrから始まります</span><span class="synConstant"></span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
</pre>
<p>
ちなみにhtml側でformの送信メソッドがgetだろうが、postだろうがCGI.pmはよきにはからってくれる。だから、頭を悩ます必要はない。
</p>
<h4>
感想
</h4>
<p>
これまでよくわからずにpostとかgetとかを見てきたけれど、こういう仕組みだったのかと納得した。
</p>
<div class="hatena-asin-detail">
<a href="http://www.amazon.co.jp/dp/4774134392/?tag=hatena_st1-22&ascsubtag=d-7ibv" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.amazon.co.jp/dp/4774134392/?tag=hatena_st1-22&ascsubtag=d-7ibv', '');"><img src="https://images-na.ssl-images-amazon.com/images/I/5182m34EgHL._SL160_.jpg" class="hatena-asin-detail-image" alt="これだけは知っておきたい Webアプリケーションの常識" title="これだけは知っておきたい Webアプリケーションの常識" /></a></p>
<div class="hatena-asin-detail-info">
<p class="hatena-asin-detail-title">
<a href="http://www.amazon.co.jp/dp/4774134392/?tag=hatena_st1-22&ascsubtag=d-7ibv" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.amazon.co.jp/dp/4774134392/?tag=hatena_st1-22&ascsubtag=d-7ibv', 'これだけは知っておきたい Webアプリケーションの常識');">これだけは知っておきたい Webアプリケーションの常識</a>
</p>
<ul>
<li>
<span class="hatena-asin-detail-label">作者:</span> <a href="http://d.hatena.ne.jp/keyword/%C6%A3%CB%DC%B0%ED" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/%C6%A3%CB%DC%B0%ED', '藤本壱');" class="keyword">藤本壱</a>
</li>
<li>
<span class="hatena-asin-detail-label">出版社/メーカー:</span> <a href="http://d.hatena.ne.jp/keyword/%B5%BB%BD%D1%C9%BE%CF%C0%BC%D2" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/%B5%BB%BD%D1%C9%BE%CF%C0%BC%D2', '技術評論社');" class="keyword">技術評論社</a>
</li>
<li>
<span class="hatena-asin-detail-label">発売日:</span> 2008/03/28
</li>
<li>
<span class="hatena-asin-detail-label">メディア:</span> 大型本
</li>
<li>
<span class="hatena-asin-detail-label">購入</span>: 10人 <span class="hatena-asin-detail-label">クリック</span>: 3,340回
</li>
<li>
<a href="http://d.hatena.ne.jp/asin/4774134392" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/asin/4774134392', 'この商品を含むブログ (14件) を見る');" target="_blank">この商品を含むブログ (14件) を見る</a>
</li>
</ul>
</div>
<div class="hatena-asin-detail-foot">
</div>
</div>
</div>