12月13日の復習

PHP フォーム復習

授業で教わったものを、復習のため再度入力してみたが、きちんと理解できておらず。
何も見ずに入力することはまだまだ不可能。。。

【繰り返し機能 (for,count)】

<?php
$title = "みんなにこんにちは!";
$names = array("鈴木","佐藤","山本","田中","伊藤");
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title><?php print($title); ?></title>
</head>
<body>
<h1><?php print($title); ?></h1>
<?php
for($i=0; $i<count($names); $i++){
	print("<p>こんにちは、".$names[$i]."さん</p>\n");
}
?>
</body>
</html>

【ドロップダウンメニューに表示する】

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ドロップダウンメニュー</title>
</head>
<body>
<?php
$city = array("東京", "名古屋", "京都", "大阪", "福岡");
?>
<h2>到着地のご案内</h2>
<form>
<select name="city">
<?php
foreach($city as $name =>$value){
	print "<option value={$value}>{$value}</option>\n";
}
?>
</select>
</form>	
</body>
</html>

【リストボックスメニューに表示する】

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>リストボックスメニュー</title>
</head>
<body>
<?php
$city = array("東京", "名古屋", "京都", "大阪", "福岡");
?>
<h2>到着地のご案内</h2>
<form>
<select name="city" size="5">
<?php
foreach($city as $name => $value){
 print "<option value={$value}>{$value}</option>\n";
}
?>
</select>
</form>
</body>
</html>

【テキストボックスから受け付ける】

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>テキストボックスからの入力を受けつける</title>
</head>
<body>
<h2>テキストボックスから受け付け</h2>
<form action="http://localhost/php/form_textbox_uketukeru1213.php" method="post">
<input type="text" name="product" />
<input type="submit" value="送信" />
</form>
<?php
if($_POST["product"])
 print "「{$_POST["product"]}」をお買い上げいただきました。<br>\n";
?>
</body>
</html>

【メニューで選択したデータを送信する】

<!DOCTYPE HTML>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>ラジオボタンからの入力を受け付ける</title>
</head>
<body>
<?php 
$product = array("鉛筆", "消しゴム", "定規", "コンパス", "ボールペン");
?>
<h2>商品をお選びください</h2>
<form action="http://localhost/php/form_menu_uketukeru1213.php" method="post">
<select name="product" size="5">
<?php
foreach($product as $value){
   print "<option value=\"{$value}\">{$value}</option>\n";
}
?>
</select>
<input type="submit" value="送信">
</form>
<?php
if($_POST["product"]){
	print "「{$_POST["product"]}」をお買い上げいただきました。\n";
}
?>
</body>
</html>

【クライアント側入力フォーム(POST)】

<!DOCTYPE HTML>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>$_POST:クライアント側入力フォーム</title>
</head>
<body>
<form action="form_$_POST1213.php" method="POST">
お名前:<input type="text" name="name" /><br />メッセージ:<input type="text" name="message" /><br /><input type="submit" value="送信" />
</form>
<?php
 $name = $_POST["name"];
 $message = $_POST["message"];
 print $name."さんは".$message."と書き込みました";
?>
</body>
</html>

【練習課題】

<!DOCTYPE HTML>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>練習課題</title>
</head>
<body>
<form action="form_rensyukadai1213.php" method="POST">
1つめ:<input type="text" name="ichi" /><br />
2つめ:<input type="text" name="ni" /><br />
<input type="submit" value="送信する" />
<?php
 $ichi = $_POST["ichi"];
 $ni = $_POST["ni"];
 print "文字列連結子で結合:";
 print $ichi.$ni."<br>\n";
 print "+で結合:";
 print $ichi + $ni; 
?>
</body>
</html>