Webデザイナーへの道しるべ | suge1040's diary

Webデザイナーになる事を目指しフェリカテクニカルアカデミーで勉強していましたsuge1040によるブログです。

5/29授業メモ PHP 

index.php

<?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'];

}

?>

 

<!DOCTYPE html>

<html lang="ja">

<head>

<meta charset="UTF-8">

<title>お問い合わせ入力フォーム</title>

<link rel="stylesheet" href="css/style.css">

</head>

<body>

<div id="container">

<h1>お問い合わせ入力フォーム</h1>

<form action="check.php" method="post" id="mailform">

<table>

<tr>

<th><label for="name">お名前:<br><span>※必須</span>&nbsp;&nbsp;30文字以内</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:<br><span>※必須</span></label></th>

<td><input type="text" name="email" id="email" size="30" placeholder="例:sample@sample.com" ></td>

</tr>

<tr>

<th><label for="tel">お電話番号:<br><span>※必須</span>&nbsp;&nbsp;半角数字</label></th>

<td><input type="text" name="tel" id="tel" size="30" placeholder="例:123-4567-8910" ></td>

</tr>

<tr>

<th><label for="message">お問い合わせ:<br><span>※必須</span>&nbsp;&nbsp;128文字以内</label></th>

<td><textarea name="message" id="message" cols="30" rows="5" placeholder="例:御社商品の資料請求を致します。" ><?php echo h($message); ?></textarea></td>

</tr>

</table>

<input type="submit" value="確認">

</form>

</div>

</body>

</html>

 

<?php

ob_end_flush();



check.php

<?php

require_once './init.php';

 

if(empty($_POST)){

 header('Location:index.php');

 exit;

}

 

$name = $_POST['name'];

$email = $_POST['email'];

$tel = $_POST['tel'];

$message = $_POST['message'];

$error = true;

 

$name_error = '';

$email_error = '';

$tel_error = '';

$message_error = '';

 

if($name==''){

 $name_error = 'お名前が、入力されていません。';

 $error = false;

}

if($email==''){

 $email_error = 'メールアドレスが、入力されていません。';

 $error = false;

}

if($tel==''){

 $tel_error = 'お電話番号が、入力されていません。';

 $error = false;

}

if($message==''){

 $message_error = 'お問い合わせ内容が、入力されていません。';

 $error = false;

}



$_SESSION['mail_data'] ['name'] = $name;

$_SESSION['mail_data'] ['email'] = $email;

$_SESSION['mail_data'] ['tel'] = $tel;

$_SESSION['mail_data'] ['message'] = $message;

 

?>

 

<!DOCTYPE html>

<html lang="ja">

<head>

<meta charset="UTF-8">

<title>お問い合わせ確認画面</title>

<link rel="stylesheet" href="css/style1.css">

</head>

<body>

<div id="container">

<h1>お問い合わせ確認</h1>

<table>

<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><a href="thanks.php">送信</a></p>

</div>

</body>

</html>

<?php

ob_end_flush();



thanks.php

<?php

require_once './init.php';

 

$name = $_SESSION['mail_data']['name'];

$email = $_SESSION['mail_data']['email'];

$tel = $_SESSION['mail_data']['tel'];

$message = $_SESSION['mail_data']['message'];

 

//セッションを取っておく必要がないのでクリア

$_SESSION['mail_data'] =array();

unset($_SESSION['mail_data']);

 

$to = 'xxxxxxxxxx@gmail.com';

$subject = 'お問い合わせメール';

 

$body =<<<BODY

【お問い合わせメール】

以下の内容で承りました。

お名前:

{$name}

メールアドレス:

{$email}

お電話番号:

{$tel}

メッセージ:

{$message}

BODY;

 

//メールのコーティング設定

mb_language('japanese');

mb_internal_encoding("UTF-8");

 

$r=mb_send_mail( $to , $subject , $body );

 

$thanks_message ='上記の内容でお問い合わせメールを承りました。';

 

if(!$r) {

$thanks_message ='メール送信エラー。以下お問い合わせは送信されませんでした。';

}



?>

 

<!DOCTYPE html>

<html lang="ja">

<head>

<meta charset="UTF-8">

<title>送信完了</title>

<link rel="stylesheet" href="css/style.css">

</head>

<body>

<div id="container">

<h1>送信完了</h1>

<p></p>

<table>

<tr>

<th>お名前:</th>

<td><?php echo h($name); ?></td>

</tr>

<tr>

<th>email:</th>

<td><?php echo h($email); ?></td>

</tr>

<tr>

<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><a href="index.php">戻る</a></p>

</div>

</body>

</html>

<?php

ob_end_flush();





☆thanks.phpに下記の赤字を記述。

$body = $name . $email . $tel . $message;

の下に、

//メールのコーティング設定

mb_language('japanese');

mb_internal_encoding("UTF-8");

 

☆thanks.phpに下記の青字を赤字に書き換える。

$body = $name . $email . $tel . $message;

↓↓↓↓↓↓↓↓

$body =<<<BODY

【お問い合わせメール】

以下の内容で承りました。

お名前:

{$name}

メールアドレス:

{$email}

お電話番号:

{$tel}

メッセージ:

{$message}

BODY;

※この命令は、入力情報をメールで送るときにそれぞれの項目を改行して表示されるようにする。

 

☆thanks.phpに赤字を記述。

$r=mb_send_mail( $to , $subject , $body );

の下に、

$thanks_message ='上記の内容でお問い合わせメールを承りました。';

 

if(!$r) {

$thanks_message ='メール送信エラー。以下にお問い合わせは送信されませんでした。';

}

※送信したときのエラー表示

※if(!$r)はif($r===FALS)の意

※if($r)はif($r===TRUE)の意




☆thanks.phpに赤字を追記。

</table><p><a href="index.php">戻る</a></p>

の間に、

<p>

<?php

if(!$r){

 echo '<span>' . h($thanks_message) . '</span>';

} else {

 echo h($thanks_message);

}

?>

</p>

 

☆index.phpの青字○○○に下記の赤字を追記。

<?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'];

}

 

☆echo と print は関数ではないので、()で囲まなくていい。

 

☆thanks.phpに赤字を追記

<tr>

<th><label for="name">お名前:<br><span>※必須</span>&nbsp;&nbsp;30文字以内</label></th>

<td><input type="text" name="name" id="name" size="30" placeholder="例:東京太郎"

></td>

</tr>

<tr>

<th><label for="name">お名前:<br><span>※必須</span>&nbsp;&nbsp;30文字以内</label></th>

<td><input type="text" name="name" id="name" size="30" placeholder="例:東京太郎"

value="<?php echo h($name); ?>"></td>

</tr>

にする。

☆thanks.phpに赤字を追記。

<tr>

<th><label for="message">お問い合わせ:<br><span>※必須</span>&nbsp;&nbsp;128文字以内</label></th>

<td><textarea name="message" id="message" cols="30" rows="5" placeholder="例:御社商品の資料請求を致します。" ></textarea></td>

</tr>

 

 

<tr>

<th><label for="message">お問い合わせ:<br><span>※必須</span>&nbsp;&nbsp;128文字以内</label></th>

<td><textarea name="message" id="message" cols="30" rows="5" placeholder="例:御社商品の資料請求を致します。" required ><?php echo h($message); ?></textarea></td>

</tr>

 

にする。

 

☆check.php に赤字を書く

<?php

require_once './init.php';

 

if(empty($_POST)){

 header('Location:index.php');

 exit;

}

 

※header('Location:index.php');は空だったら index.php に飛ばすの意

※exit;は処理を終了の意

 

☆check.php に赤字を書く

$name = $_POST['name'];

$email = $_POST['email'];

$tel = $_POST['tel'];

$message = $_POST['message'];

$error = true;



$name_error = '';

$email_error = '';

$tel_error = '';

$message_error = '';



if($name==''){

 $name_error = 'お名前が、入力されていません。';

 $error = false;

}

if($email==''){

 $email_error = 'メールアドレスが、入力されていません。';

 $error = false;

}

if($tel==''){

 $tel_error = 'お電話番号が、入力されていません。';

 $error = false;

}

if($message==''){

 $message_error = 'お問い合わせ内容が、入力されていません。';

 $error = false;

}



※$name_error = ' '; の ’ ’ は「空文字を代入される。」の意。



☆check.php に赤字を追記。

<table>

<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>



☆index.php の 赤字 を削減

<td><textarea name="message" id="message" cols="30" rows="5" placeholder="例:御社商品の資料請求を致します。" required ><?php echo h($message); ?></textarea></td>

↓↓↓↓↓↓

<td><textarea name="message" id="message" cols="30" rows="5" placeholder="例:御社商品の資料請求を致します。" ><?php echo h($message); ?></textarea></td>