65回目授業内容〜
[授業][Dreamweaver][PHP]
フォームの内容が空の場合に警告
index.html
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>フォームの内容が空の場合に警告を表示する</title> <link rel="stylesheet" href="style.css" media="all"> </head> <body> <div id="wrap"> <div id="head"> <h1>フォームの内容が空の場合に警告を表示する</h1> </div> <div id="content"> <form action="input.php" method="post"> <dl> <dt>名前<span class="required">必須</span></dt> <dd> <input id="my_name" type="text" name="my_name" size="35" maxlength="255"> </dd> </dl> <input type="submit" value="送信する"> </form> </div> </div> </body> </html>
input.php
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>フォームの内容が空の場合に警告を表示する</title> <link rel="stylesheet" href="style.css" media="all"> </head> <body> <div id="wrap"> <div id="head"> <h1>フォームの内容が空の場合に警告を表示する</h1> </div> <div id="content"> <p> <?php if (empty($_POST['my_name'])) { print '名前を記入して下さい'; } else { print '正しく記入されています'; } ?> </p> </div> </div> </body> </html>
複数選択可能なチェックボックスの値を取得する
index.html
<div id="wrap"> <div id="head"> <h1>複数選択可能なチェックボックスの値を取得する</h1> </div> <div id="content"> <form action="input1.php" method="post"> <dl> <dt>ご予約希望日(複数選択可):</dt> <dd> <ul> <li><input id="reserve_1" type="checkbox" name="reserve[]" value="1/1"><label for="reserve_1">1月1日</label></li> <li><input id="reserve_1" type="checkbox" name="reserve[]" value="1/2"><label for="reserve_2">1月2日</label></li> <li><input id="reserve_1" type="checkbox" name="reserve[]" value="1/3"><label for="reserve_3">1月3日</label></li> </ul> </dd> </dl> <input type="submit" value="送信する"> </form> </div> </div>
input1.php
<div id="wrap"> <div id="head"> <h1>複数選択可能なチェックボックスの値を取得する</h1> </div> <div id="content"> <p> ご予約:<br> <?php foreach($_POST['reserve'] as $reserve) { print(htmlspecialchars($reserve, ENT_QUOTES)); } ?> </p> </div> </div>
半角数字に直して数字であるかをチェックする
<div id="wrap"> <div id="head"> <h1>半角数字に直して、数字であるかをチェックする</h1> </div> <div id="content"> <form action="input2.php" method="post"> <dl> <dt>年齢(数字でご記入ください)</dt> <dd><input type="text" name="age" size="5" maxlength="3">歳</dd> </dl> <input type="submit" value="送信する"> </form> </div> </div>
input3.php
<p> <?php $age = mb_convert_kana($_POST['age'], 'n' , 'UTF-8'); if (is_numeric($age)){ print ($age . '歳'); } else { print '※年齢は数字でご記入ください'; } ?> </p>
郵便番号を正規表現を使ってチェックする
<div id="wrap"> <div id="head"> <h1>郵便番号を正規表現を使ってチェックする</h1> </div> <div id="content"> <form action="input3.php" method="post"> <dl> <dt>郵便番号(例: 123-4567)</dt> <dd><input type="text" name="zip" size="10" maxlength="8"></dd> </dl> <input type="submit" value="送信する"> </form> </div> </div> input3.php >|php| <div id="wrap"> <div id="head"> <h1>郵便番号を正規表現を使ってチェックする</h1> </div> <div id="content"> <?php $zip = mb_convert_kana($_POST['zip'], 'a' , 'UTF-8'); if (preg_match("/\A\d{3}\-\d{4}\z/", $zip)) { print '記入は正しいです。'; } else { print ('郵便番号は123-4567の形式でご記入下さい'); } ?> </div> </div>
入力されたIDを次回アクセスまで覚える
<?php if(isset($_COOKIE['my_id'])) { $myid = $_COOKIE['my_id']; } else { $myid = ''; } ?> <!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>入力されたIDを次回アクセス時まで覚える</title> <link rel="stylesheet" href="style.css" media="all"> </head> <body> <div id="wrap"> <div id="head"> <h1>入力されたIDを次回アクセス時まで覚える</h1> </div> <div id="content"> <form action="input4.php" method="post"> <dl> <dt>ID</dt> <dd><input type="text" name="my_id" id="my_id" value="<?php echo $myid; ?>"></dd> <dt>パスワード</dt> <dd><input type="password" name="password" id="password"></dd> <dt>ID保存</dt> </dl> <p><input type="checkbox" name="save" id="save" value="on"><label for="save">IDを保存する</label></p> <input type="submit" value="送信する"> </form> </div> </div> </body> </html>
input.php
<?php $myid = $_POST['my_id']; $password = $_POST['password']; $save = $_POST['save']; // Cookieに保存 if ($save =='on') { setcookie('my_id', $myid, time() + 60 * 60 *24 * 14 ); $message = 'ログイン情報を記録しました'; } else { setcookie('my_id'); $message = '記録しませんでした'; } ?> <!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>入力されたIDを次回アクセス時まで覚える</title> <link rel="stylesheet" href="style.css" media="all"> </head> <body> <div id="wrap"> <div id="head"> <h1>入力されたIDを次回アクセス時まで覚える</h1> </div> <div id="content"> <p> <p><?php echo $message; ?></p> <p><a href="index4.php">戻る</a></p> </p> </div> </div> </body> </html>