My Web道

WEB制作科 受講記録 …とその後も続くWEB制作に関する活動・学習記録です。

※ 当サイトはアフィリエイト広告を利用しています

Bootstrap サイトにメールフォームを作成

f:id:sntkk3:20170814211805j:plain

Bootstrapテンプレートで作成したサイトに、
メールフォームを設置しました。


《index.php

以下を、htmlの直前に記述

<?php 
require_once(dirname(__FILE__)).'/init.php';
//require_once 'init.php';

$name = '';
$email = '';
$tel = '';
$message = '';
if(isset($_SESSION['mail_data'])) {
  $name = $_SESSION['mail_data']['name'];
  $email = $_SESSION['mail_data']['email'] ;
  $tel = $_SESSION['mail_data']['tel'] ;
  $message = $_SESSION['mail_data']['message']; 
}
?>


《index.php》 お問い合わせフォーム

<div id="contact">
    
    <h1>Contact</h1>
<form action="check.php" method="post" id="mailform">
<table>
<tr>
<th><label for="name">お名前:&nbsp;&nbsp;<em></em></label></th>
<td><input type="text" name="name" id="name" size="30" placeholder="例:鈴木一郎" value = "<?php echo h($name); ?>" ></td>
</tr>
<tr>
<th><label for="email">email:&nbsp;&nbsp;<em></em>&nbsp;&nbsp;</label></th>
<td><input type="text"  name="email" id="email" size="30" placeholder="例:sample@sample.com" value ="<?php echo h($email); ?>"></td>
</tr>
<tr>
<th><label for="tel">お電話番号:</label></th>
<td><input type="text" name="tel" id="tel" size="30" placeholder="例:123-4567-8910(半角数字)" value ="<?php echo h($tel); ?>"></td>
</tr>
<tr>
<th><label for="message">お問い合わせ:&nbsp;&nbsp;<em></em></label></th>
<td><textarea name="message" id="message" cols="30" rows="5" placeholder="お仕事のご依頼・お問い合わせ"><?php echo h($message); ?></textarea></td>
</tr>
<tr><th></th><td><input type="submit" class="btn" value="確認"></td></tr>
</table>

</form>
    
</div>


《init.php

<?php
function h($str) {
 return htmlspecialchars($str, ENT_QUOTES, "UTF-8");	
}
header('X-Frame-OPTIONS:DENY');
ob_start();
session_start();
session_regenerate_id(true);


《check.php

<?php 
require_once(dirname(__FILE__)).'/init.php';
//require_once 'init.php';

//空データチェック
//データがなければ先頭(入力)ページに戻す
if(empty($_POST)) {
  header('Location:index.php');
  exit;	
}

//var_dump($_POST);
//print_r($_POST);
$name = $_POST['name'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$message = $_POST['message'];
$error = true;
//echo '('.$name.')';
$name_error = '';
$email_error = '';
$tel_error = '';
$message_error = '';


if( mb_strlen($name) > 30 ) {
  $name_error = 'お名前の入力文字数が多すぎます。';
  $error = false;	
}
//受け取れるメールアドレスの限界値は320
//ローカルパート(@の前は64文字)
if( mb_strlen($email) > 50 ) {
  $email_error = 'メールアドレスの入力文字数が多すぎます。';
  $error = false;	
}

//

if( mb_strlen($tel) > 13 ) {
  $tel_error = '電話番号の入力文字数が多すぎます。';
  $error = false;	
} 

if( mb_strlen($message) > 128 ) {
  $message_error = 'お問い合わせ内容の入力文字数が多すぎます。';
  $error = false;	
}

//メールアドレス@チェック
if(strpos($email,'@')===false) {
  $email_error = '不正なメールアドレスです。';
  $error = false;	
}

//半角入力チェック(email)
if(strlen($email) !==mb_strlen($email, "UTF-8")){
	$email_error = 'すべて半角数字で入力してください。';
	$error = false;
} 

//半角入力チェック(電話番号)
if(strlen($tel) !==mb_strlen($tel, "UTF-8")){
	$tel_error = 'すべて半角数字で入力してください。';
	$error = false;
} 

//空文字チェック
if($name =='') {
  $name_error = 'お名前が、入力されていません。';
  $error = false;	
}
if($email =='') {
  $email_error = 'メールアドレスが、入力されていません。';
  $error = false;	
}
/*if($tel =='') {
  $tel_error = 'お電話番号が、入力されていません。';
  $error = false;	
}*/
if($message =='') {
  $message_error = 'メッセージが、入力されていません。';
  $error = false;	
}

//エラーチェックをこの直前に挿入(6/5)
$_SESSION['mail_data']['name'] = $name;
//var_dump($_SESSION['mail_data']['name']);
$_SESSION['mail_data']['email'] = $email;
//var_dump($_SESSION['mail_data']['email']);
$_SESSION['mail_data']['tel'] = $tel;
//var_dump($_SESSION['mail_data']['tel']);
$_SESSION['mail_data']['message'] = $message;
//var_dump($_SESSION['mail_data']['message']);
$_SESSION['mail_data']['error'] = $error;
?>

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>お問い合わせ確認</title>
<link rel="stylesheet" href="css/stylish-portfolio.css">
<style>
 table {
   /*border-collapse: collapse;*/
 }

</style>
</head>
<body>
<div id="contact">
<h1>お問い合わせ内容(確認画面)</h1>
<table id="frmTbl" border="1px">
<tr>
<th>お名前:</th><td><?php echo h($name).h($name_error); ?></td>
</tr>
<tr>
<th>email:</th><td><?php echo h($email).h($email_error); ?></td>
</tr>
<tr>
<th>お電話番号:</th><td><?php echo h($tel).h($tel_error); ?></td>
</tr>
<tr>
<th>お問い合わせ内容:</th><td><?php echo nl2br(h($message).h($message_error)); ?></td>
</tr>
</table>
<p><a href="index.php">戻る</a>
<?php 
if($error) {
echo '<a href="thanks.php">送信</a>';
}
?>
</p>
</div><!--contact-->
</body>
</html>
<?php ob_end_flush(); 


《thanks.php

<?php
require_once(dirname(__FILE__)).'/init.php';
//require_once 'init.php';
//var_dump($_SESSION);
if(!$_SESSION['mail_data']['error']) {
	header('Location:index.php');
	exit;
}

//セッションから値を取得
$name = $_SESSION['mail_data']['name'];
$email = $_SESSION['mail_data']['email'] ;
$tel = $_SESSION['mail_data']['tel'] ;
$message = $_SESSION['mail_data']['message']; 
//$data = $_SESSION['mail_data'];
//var_dump($data);

//セッションを取っておく必要がないのでクリア
$_SESSION['mail_data'] = array();
unset($_SESSION['mail_data']);

if(isset($_COOKIE[session_name()] )) {
setcookie( session_name(), '', time()-42000, '/');	
}
session_destroy(); //セッションを破棄する


//クライアントへのメール送信内容の設定
$to = "XXXXXX@yahoo.co.jp"; //メール送信宛先
$subject = 'お問い合わせ';
$from = $email;
//$body = $name . $email . $tel . $message;

$body = <<< BODY
【お問い合わせメール】

 以下内容で承りました。

  お名前: {$name}

  メールアドレス: {$email}

  お電話番号: {$tel}

  
  メッセージ:
  
   {$message}


BODY;

//メールのエンコーディング設定
mb_language('japanese');
mb_internal_encoding('UTF-8');

$r = mb_send_mail($to, $subject, $body);
//var_dump($r);
//mb_send_mail("送信先","表題","本文","ヘッダオプション");
$thanks_message = '上記の内容でお問い合わせメールを承りました。';
if(!$r) {
 $thanks_message = 'メール送信エラー。以下お問い合わせは送信されませんでした。';
}
?>

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>送信終了</title>
<link rel="stylesheet" href="css/stylish-portfolio.css">
<style>
 th {
   backgroud: #D3D3D3;
 }
</style>
</head>
<body>
<div id="contact">
<h1>送信終了</h1>

<table id="frmTbl" border="1px">
<tr class="small">
<th>お名前:</th>
<td><?php echo h($name); ?></td>
</tr>
<tr class="small">
<th>email:</th>
<td><?php echo h($email); ?></td>
</tr>
<tr class="small">
<th>お電話番号:</th>
<td><?php echo h($tel); ?></td>
</tr>
<tr>
<th>お問い合わせ内容:</th>
<td><?php echo nl2br(h($message)); ?></td>
</tr>
</table>
<p>
<?php
if(!$r) {
  echo '<span>'.h($thanks_message).'</span>';
} else {
  echo h($thanks_message);	
}
?>
</p>
<p id="sbubutton"><a href="index.php">戻る</a></p>
</div><!--contact-->
</body>
</html>
<?php ob_end_flush(); 

※ 当サイトはアフィリエイト広告を利用しています