Home > PHP > PHP - フォーム処理

PHP - フォーム処理

  • 2012-03-13 (Tue) 09:20
  • PHP
春の陽気が少しずつですが、感じられますね。
2012年が始まって、もう三月というのには驚いてしまいますが、年を取るごとに1年が短く感じられるようです。

この前、「はやぶさ/HAYABUSA」という映画を見ました。この映画内容に関しては、他にも2作品あるようなのですが、見比べるのも面白いかもしれません。
感想は、最後をどのように持っていくか・・というところに注目してしまい、「結局はそういくしかないなぁ」っと思いました。他の2作品はどのようになるのかが楽しみですが、実際の話なので、終わりは一つしかないのでしょうか・・と。^^;

今日はPHPの第3回目・・フォームの処理についてです。

第3回 フォーム処理
PHPの主な使い方は、HTMLフォーム処理です。基本概念は、あるフォーム内のすべてのフォーム要素が自動的にPHPスクリプトで利用できるという事です。

最もポピュラーな作り方は、フォームのページとその結果を出力するためのページを用意します。フォームページは通常のHTMLですが、PHPでも構いません。但し、出力する方のページはPHPのページになります。

では、サンプルコードを見てみます。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>サンプル</title>
</head>
<body>
<form action="mypage.php" method="post">
<p>お名前:<input type="text" name="name" size="30" /><br />
年齢:<input type="text" name="age" size="30" /></p>
<p><input type="submit" value="Click" /></p>
</body>
</html>

以上のコードを記述して、適当な名前で保存します。もちろん、Webサーバ内のhtdocsフォルダ内に保存します。

このサンプルコードは、一般的なフォームです。
まず、action属性の値に、出力するページを指定します。ここでは、mypage.phpというページにこのフォームの値を与えることになります。
method属性は、「post」にします。method属性の値には、他に「get」があります。postとgetの違いは、また機会があれば説明しますが、通常はpostにします。a要素のhref属性から呼び出すときは、getを使うことになります。

フォームの記述で大事なポイントは、ユーザが入力した値を処理するためには、その要素にname属性を必ず付けることです。このname属性の値で、phpは処理します。

次に出力先のmypage.phpのコードを見てみます。
<?php
 print "こんにちは" . $_POST["name"] . "さん。あなたは" . $_POST["age"] . "歳です。";
?>

シンプルなものですが、$_POST["name"]は、フォームのmethodでpostを指定しているからです。簡単に言うと、フォームからPOSTで送られてきたという意味と思ってください。
その引数内に、「name」がありますが、これはフォームのname属性の値である「name」の値が格納されていますので、ユーザが入力した名前になります。
$_POST["age"]も同様な意味で、ユーザが入力した年齢となります。

また、「.」は文字列を連結するための記号です。

では、処理を見ていきます。まず、sample.phpをブラウザで表示します。そして、名前と年齢を入力します。
20120313-p08.jpg

ボタンをクリックすると、mypage.phpにジャンプしてPHPの処理が実行された出力が表示されます。
20120313-p09.jpg

先ほどまでは、sample.phpからmypage.phpへの出力を行いましたが、一般的な使い方として、自ページから自ページに対して処理を行うこともします。
では、その例を見ていきます。内容は先ほどの内容を同じになります。
<?php
if(isset($_POST["go"])){
print "こんにちは" . $_POST["name"] . "さん。あなたは" . $_POST["age"] . "歳です。";
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>サンプル</title>
</head>
<body>
<form action="sample.php" method="post">
<p>お名前:<input type="text" name="name" size="30" /><br />
年齢:<input type="text" name="age" size="30" /></p>
<p><input type="submit" name="go" value="Click" /></p>
</body>
</html>


フォーム内のaction属性の値を見ると、同ページを指定しています。つまり、フォーム内の処理を自ページに値を処理させています。
その処理するのがphpタグ内の処理です。

先ほどと異なるのが、if構文を使っている点で、もしもフォームのボタンが押されたら、それをprint(出力)させることです。
もし、if構文を使わなかったら、常にフォームの上に「こんにちはさん。あなたは歳です。」が表示されることになります。

では、処理を見ていきます。
sample.phpにアクセスしてブラウザで表示し、名前と年齢を入力します。
20120313-p10.jpg

入力したら、ボタンをクリックします。
20120313-p01.jpg
結果がフォームの上に表示されます。

使い方からすると、最初のフォームの使い方が妥当だと感じられますが、実際は自ページに対して処理を行い、if構文でページを振り分けることを良く行います。

少し余談ですが、先ほどのphpコードの中の$_POST[""]内の値は文字列であったり、数値であったりします。また、悪意的にフォーム内にJavascriptを埋め込まれることも考えられます。そこで、そのような対処として次のような記述を行えます。
$_POST["name"] → htmlspecialchars($_POST["name"])
$_POST["age"] → (int)$_POST["age"]

htmlspecialchars()は、htmlでの特殊なコードを適切にエンコードしてくれますので、javascriptを埋め込まれないようにできます。
年齢は数値であることは分かっているので、年齢の値を整数(integer型)に変換します。これにより、おかしな文字が入力されることを防げます。

最初はピンと来ないかもしれませんが、徐々に構文や関数を覚えていくと、理解できます。PHPはとても柔軟なプログラムなので、初心者にとって分かり易いかと思います。

Comments:12

wlp 2015-06-29 (Mon) 18:13

http://www.guccihandbags.com.co/
http://www.gucci-outlet.in.net/
http://www.gucci--outlet.com.co/
http://www.guccishoes.net.co/
http://www.guccishoes.us.org/
http://www.hermesbags.com.co/
http://www.hermesbirkin.com.co/
http://www.hermesoutlet.net.co/
http://www.hollister.us.org/
http://www.hollisterclothing-store.in.net/
http://www.insanityworkout.net.co/
http://www.iphone-cases.us/
http://www.ralphlaurenpolo.in.net/
http://www.ray-ban-outlet.us.com/
http://www.raybans.us.org/
http://www.rayban-sunglasses.org.uk/
http://www.rayban-sunglasses.us.org/
http://www.raybansunglassesoutlet.net.co/
http://www.raybanwayfarer.in.net/
http://www.replicahandbags.com.co/
http://www.replicawatches.us.com/
http://www.retro-jordans.com/
http://www.rolex-watches.me.uk/
http://www.rosherun.org.uk/
http://www.rosheruns.us/
http://www.salvatoreferragamo.in.net/
http://www.soccer-shoes.org/
http://www.softball-bats.us/
http://www.suprashoe.net/
http://www.swarovskicrystal.com.co/
http://www.swarovskijewelry.com.co/
http://www.swarovski-uk.org.uk/
http://www.the-northface.com.co/
http://www.the-northface.in.net/
http://www.thenorth-face.org.uk/
http://www.thenorthface.us.org/
http://www.thenorthfacejackets.in.net/
http://www.thomassabo-uk.org.uk/
http://www.tiffanyandco.net.co/
http://www.tiffanyjewelry.us.org/
http://www.tory-burch-outlet.in.net/
http://www.tory-burchoutlet.us.com/
http://www.louboutin.jp.net/
http://www.louis-vuittoncanada.ca/
http://www.louisvuitton.jp.net/
http://www.louis--vuitton.org.uk/
http://www.louisvuitton.so/
http://www.louisvuittonas.com/
http://www.edhardy.in.net/
http://www.levisjeans.com.co/
http://www.bcbgdresses.net/
http://www.bebeclothing.net/
http://www.harrods-london.co.uk/
http://www.guccishoes.com.co/
http://www.ralphlaurenoutletonline.us.org/
http://www.true-religion.com.co/
http://www.truereligionjeans.net.co/
http://www.truereligion-outlet.com.co/
http://www.uggaustralia.net.co/
http://www.uggboots.net.co/
http://www.uggbootsclearance.com.co/
http://www.uggsonsale.com.co/
http://www.uggsoutlet.com.co/
http://www.uptocoachoutlet.com/
http://www.vansshoes.us/
http://www.weddingdressesuk.org.uk/
http://www.yogapants.com.co/
http://www.ugg-boots.us.org/
http://www.poloralphlaurenoutlet.net.co/
http://www.burberryoutletonline.ar.com/
http://www.toms-outlet.net.co/
http://www.michaelkors.in.net/
http://www.christianlouboutinoutlet.net.co/
http://www.toryburchsale.com.co/
http://www.pradaoutlet.com.co/
http://www.longchamp-handbags.in.net/
http://www.longchampoutlet.com.co/
http://www.chanel-bags.com.co/
http://www.truereligion-outlet.us.org/
http://www.abercrombie-and-fitch.us.com/
http://www.timberlandboots-outlet.net/
http://www.timberland-shoes.com/
http://www.tommyhilfiger.net.co/
http://www.tommy-hilfigeroutlet.com/
http://www.tomshoesoutlet.com/
http://www.toms-outlet.in.net/
http://www.toms-shoes.com.co/
http://www.hollisterclothing.in.net/
http://www.newbalance-shoes.org/
http://www.converse--shoes.net/
http://www.lululemonoutlet.com.co/
http://www.nfl-jerseys.in.net/
http://www.cheapjerseys.us.org/
http://www.rolex-watches.us.com/
http://www.rolexwatchesforsale.us.com/
http://www.p90xworkout.in.net/
http://www.giuseppezanotti.com.co/
http://www.maccosmetics.net.co/
http://www.instyler.in.net/
http://www.mizunorunning.net/
http://www.handbagsoutlet.com.co/
http://www.hilfigeroutlet.in.net/
http://www.kate-spade.com.co/
http://www.katespade-outlet.com.co/
http://www.kate-spades.com/
http://www.longchamp.us.org/
http://www.longchamp.com.co/

http://support.Humancoder.com/ 2017-12-13 (Wed) 11:08

Ꮇy brother recommended I may likе this web site.
Ηe wass totally riցht. This post aсtually
mаde my ԁay. Yoս сɑn not imagine just how a lot time I һad spent foг this infοrmation! Tһank you!

Captchaboss 2017-12-28 (Thu) 18:26

wonderful points altogether, you јust won a emblem neᴡ reader.
Wһаt could y᧐u sugges abnout үߋur post that yoou siimply mɑde а few
days ago? Аny sure?

bypass captchas 2017-12-28 (Thu) 19:41

I ɑm in fact thankful tߋ the holder оf tһiѕ web sitee ᴡho hаѕ shared thiѕ impressive
articlee at at tһis time.

ExpertDecoders 2017-12-29 (Fri) 00:42

What'ѕ Happening і am new to thіs, I stumbled upon this I
һave found It positively useful aand it hɑs helped me out
loads. I am hoping tο gіve a contribution & assist ᧐ther useгs
ⅼike its helped me. Great job.

remove google Captcha 2017-12-29 (Fri) 04:54

Wonderful goods from you, man. I have understand youг stuff previoսs tο and you'rе just too great.
Ι гeally like ѡhɑt yߋu һave acquired here, гeally ⅼike what you'rе stating ɑnd tһe way іn ѡhich you saү it.
You make it entertaining and you ѕtill care for to kеep іt wise.
I cant wait tо rеad mucһ morе from ʏou. Thhis is really a
great website.

https://gomibet.com 2017-12-30 (Sat) 05:40

Now that you be familiar with video editing along with the what you require you are to begin right onto your
pathway of amateur filmmaker to professional
director. These guides let you practice when you are ready and also have the
time to do so. You need a special connector typically
known as a Fire wire or commonly known as as an IEEE 1394 high band
connector.

pussy 2018-01-08 (Mon) 21:32

You can even replace your favorite MP3 music with your to ensure that if you are concerning the gym, you'll be
able to still understand interesting points from your book or listen for the docs from perform which you should examine.
" It was President Theodore Roosevelt who had given it the category of White House in 1901. You need a special connector typically known as a Fire wire or sometimes known as an IEEE 1394 high band connector.

link 188bet 2018-01-09 (Tue) 04:27

Now that you learn about video editing along with the what you require you
are prepared to start right onto your pathway of amateur filmmaker to professional director.
" It was President Theodore Roosevelt who had given it the naming of White House in 1901. Here you are able to shop by theme or browse a whole array of themes if you're sill unsure on the to base the party.

dragon ball super 123 vostfr youtube 2018-02-06 (Tue) 06:40

Les promotions sur Dragon ball super sont récurrentes.

Captcha killer 2018-02-22 (Thu) 01:48

Hi there to all, the contents existing
ɑt this web site are ɑctually awesome for people knowledge, ᴡell,
keep ᥙp the ɡood ԝork fellows.

https://www.detodocoahuila.Com/ 2018-02-22 (Thu) 04:56

Pretty! This ѡas аn incredibly wonderful post.
Тhank yⲟu ffor supplying tһis information.

Comment Form
★下記に2つの英単語をスペースで区切って入力してください

Home > PHP > PHP - フォーム処理

Search
Feeds

Page Top