53回目授業内容 PHP〜脱線
[授業][Dreamweaver][JavaScript]
php
- カウンタ変数をループ内で使う
<?php for ($i=0; $i < 5; $i++){ print '商品' . $i . 'を表示します。<br>' . "\n"; } print '商品は以上です。' . "\n"; ?>
- for文を応用する
<table border="1"> <tr><th>イメージ</th><th>商品名</th></tr> <?php for ($i=0; $i<6; $i++){ print '<tr>'; print '<td><img src="'. $i . '.jpg"></td><td>商品' . $i . '</td>'; print '</tr>' . "\n"; } ?>
- 指定個数の画像を表示する
<?php $num = 6; for ($i=0; $i<$num; $i++){ print '<img src="' . $i . '.jpg">' . "\n"; } ?>
- while文
<?php $i = 0; while($i < 5){ print ('商品は' . $i . 'を表示します。<br>' . "\n"); $i++; } print '商品は以上です。' . "\n"; ?>
フォームのデータを受け取る
- テキストを送信すると表示
<?php print '入力内容:'.@$_POST['text1']; ?> <form method="POST" action="<?php echo $_SERVER['PHP_SELF']?>"> <table> <tr> <td><input type="text" name="text1"></td> <td><input type="submit" value="送信" name="sub1"></td> </tr> </table> </form>
- フォームのデータを計算させる
<form method="post" action="<?php print $_SERVER['PHP_SELF']?>" name="form1"> <input type="text" name="txtA"> + <input type="text" name="txtB"> = 結果 <input type="submit" value="計算"> <br><br> <?php $a = $_POST['txtA']; $b = $_POST['txtB']; $c = $a+$b; print '計算結果:' . $c; ?> </form>
- フォームに入力した文字を別のphpで書き出す
<form action="ans.php" method="get"> <label for="my_name">お名前:</label> <input id="my_name" type="text" name="my_name" size="35" maxlength="255" value=""> <input type="submit" value="送信する"> </form>
ans.php
<?php print 'お名前:' . htmlspecialchars($_POST['my_name'], ENT_QUOTES); ?>
-
- postとrequestは同じ動作をする。
- getはURLの後ろに「?」を漬けて値を指定して送信する。
- 重要な会員登録・お問合せ等のページでは「get」は使わず「post」を使う
- GETの場合
- POSTの場合
- ラジオボタンの値を取得する
<form action="<?php print $_SERVER['PHP_SELF']?>" method="post"> <dl> <dt>性別:</dt> <dd> <input id="gender_male" type="radio" name="gender" value="male"><label for="gender_male">男性</label> <input id="gender_female" type="radio" name="gender" value="female"><label for="gender_female">女性</label> </dd> </dl> <input type="submit" value="送信する"> </form> <br><br> <?php print '性別:' . htmlspecialchars($_POST['gender'], ENT_QUOTES); ?>
- ドロップダウン
<form action="<?php print $_SERVER['PHP_SELF']?>" method="post"> <select name="age" id="age"> <?php for ($i=10; $i<=70; $i++){ print '<option value="' . $i . '">' . $i . '歳</option>'; } ?> </select> <input type="submit" value="送信する"> </form> <br><br> <?php print '性別:' . htmlspecialchars($_POST['age'], ENT_QUOTES); ?>
- 日にちをドロップダウン
<form action="<?php print $_SERVER['PHP_SELF']?>" method="post"> <dl> <dt>日にち</dt> <dd> <?php print(date('n'));?>月 <select name="day" id="day"> <?php for ($day=1; $day<=date('t'); $day++){ print '<option value="' . $day . '">' . $day . '日</option>'; } ?> </select> </dd> </dl> <input type="submit" value="送信する"> </form> <br><br> <?php print '今日は:' . (date('n')) . '月' . htmlspecialchars($_POST['day'], ENT_QUOTES) . '日です。'; ?>
- 日にちをドロップダウン
<form action="<?php print $_SERVER['PHP_SELF']?>" method="post"> <dl> <dt>日にち</dt> <dd> <?php print(date('n'));?>月 <select name="day" id="day"> <?php for ($day=1; $day<=date('t'); $day++){ print '<option value="' . $day . '">' . $day . '日</option>'; } ?> </select> </dd> </dl> <input type="submit" value="送信する"> </form> <br><br> <?php print '今日は:' . (date('n')) . '月' . htmlspecialchars($_POST['day'], ENT_QUOTES) . '日です。'; ?> -フォームのデータを配列で受け取る <?php for ($i=0; $i < count(@$_POST['check1']); $i++){ print '<p>' . $_POST['check1'][$i] . '選択されました。'; } ?> <form method="post" action="<?php print $_SERVER['PHP_SELF']?>"> <table> <tr> <td> <input type="checkbox" name="check1[]" value="音楽">音楽 <input type="checkbox" name="check1[]" value="旅行">旅行 <input type="checkbox" name="check1[]" value="スポーツ">スポーツ <input type="checkbox" name="check1[]" value="映画">映画 </td> </tr> <tr> <td> <input type="submit" value="送信する" name="sub1"> </td> </tr> </table> </form>