2011年4月20日水曜日

HTMLフォームからの送信

<html>
<head>
<title>10日でおぼえるPHP5入門教室(アンケート)</title>
</head>
<body>
<h1 style="background:#cccccc">
10日でおぼえるPHP5入門教室(アンケート)</h1> 
<form method="POST" action="sendmail.php">
<input type="hidden" name="_email" value="k.narita@naritan.com" />
<input type="hidden" name="_subject" value="10日でおぼえるPHP5入門教室" />
<input type="hidden" name="_result"
  value="<?php print($_SERVER['PHP_SELF']);?>" />
<table border="0">
 <tr>
 <th align="right">名前</th>
 <td><input type="text" name="名前" size="20" maxlength="30" /></td>
 </tr><tr>
 <th align="right">E-Mail</th>
 <td><input type="text" name="_from" size="50" maxlength="255" /></td>
 </tr><tr>
 <th align="right" valign="top">自由記入欄</th>
 <td><textarea name="内容" cols="50" rows="5"></textarea></td>
 </tr>
</table>
<input type="submit" value="送信" />
<input type="reset" value="クリア" />
</form>
</body>
</html>

※$_SERVER['PHP_SELF']とは、実行中のスクリプトのドキュメントルートに対する相対パス
※PHPスクリプトなどで、自身にフォームを送信したい場合などにactionにこの変数を設定することが多いらしい。

<?php
$header_info="From: ".$_POST['_from']."\nContent-Type: text/plain;charset=ISO-2022-JP\nX-Mailer: PHP/".phpversion();
$body="".$_POST['_subject']."\n\n";
foreach($_POST as $key=>$value){
 if(!strstr($key,"_")){
  $body.="".$key."".$value."\r\n";
 }
}
mb_send_mail($_POST['_email'],$_POST['_subject'],$body,$header_info);
header("Location: ".$_POST['_result']);
?>
mb_send_mail(宛先、件名、本文、追加ヘッダ)

0 件のコメント:

コメントを投稿