name属性に[]を付けると、配列を使って複数の値を取得出来る
name属性に[]を付けると、複数送信できる
var_dump($color);を実行すると
array (size=2)
0 => string '青' (length=3)
1 => string '黄' (length=3)
と表示される
■test05.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xm1ns="http://www.w3.org/1999/xhtml">
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>PHP入門</title>
</head>
<body>
<form action="test05_output.php" method="post">
<p>
<input type="text" name="name">
</p>
<?php
$color = array (
"赤",
"青",
"黄"
);
foreach ( $color as $value ) {
echo "$value" . '<input type="checkbox" name="color[]" value="' . $value . '">';
}
?>
<input type="submit" value="送信">
</body>
</html>
■test05_output.php
```php
<?php
$name = $_POST ["name"];
$color = $_POST ["color"];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xm1ns="http://www.w3.org/1999/xhtml">
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>PHP入門</title>
</head>
<body>
<p><?php echo $name;?></p>
<p><?//php echo $color; ?></p>
<?php
foreach ( $color as $value ) {
echo $value;
}
?>
</body>
</html>
デフォルトありver
■test13_input.php
<?php
session_start ();
if (isset ( $_SESSION ['err'] )) {
$err = $_SESSION ['err'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xm1ns="http://www.w3.org/1999/xhtml">
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript"
src="https://ajaxzip3.github.io/ajaxzip3.js">
<!--
function disp(){
// 「OK」時の処理開始 + 確認ダイアログの表示
if(window.confirm('本当にいいんですね?')){
location.href = "confirm.php"; // example_confirm.html へジャンプ
}
// 「OK」時の処理終了
// 「キャンセル」時の処理開始
else{
window.alert('キャンセルされました'); // 警告ダイアログを表示
}
// 「キャンセル」時の処理終了
}
// -->
</script>
<title>Contact</title>
<style type="text/css">
p#submit_btn {
text-align: center;
}
.content {
/* margin-left: 137px; */
margin-top: 0px;
font-size: 95%;
font-weight: bold;
}
.form {
color: white;
background-color: #ad3140;
text-align: center;
/* padding-left: 370px; */
padding-top: 5px;
padding-bottom: 5px;
font-weight: bold;
font-size: 95%;
width: 825px;
}
.textform {
margin-left: 68px;
}
.textform_postalCode {
margin-left: 30px;
}
.textstyle {
font-size: 95%;
font-weight: bold;
font-family: font-family : "MS Pゴシック", "MS ゴシック", sans-serif;
}
.required {
font-size: 87%;
font-weight: normal;
color: red;
}
# heading {
border-left: 10px solid #CC3366;
border-bottom: 1px dashed #CC3366; /* 下ボーダーを、1px幅の破線、線色#CC3366に */
}
</style>
</head>
<body>
<h1 id="heading">アンケート</h1>
<!-- <h1>アンケート</h1> -->
<form action="test13_output.php" method="post">
■趣味はなんですか?<span class="required">(必須)複数選択可</span> </br>
<?php
$object = array (
"ゲーム",
"ショッピング",
"映画",
"旅行",
"上記以外"
);
foreach ( $object as $value ) {
if ((isset ( $_SESSION ['object'] ) && in_array ( $value, $_SESSION ['object'] )) || (! isset ( $_SESSION ['object'] ) && $value == "上記以外")) {
echo '<label><input type="checkbox" name="object[]" value="', $value, '"checked><font style="margin-left: 40px;">', $value, '</font></label><br>';
} else {
echo '<label><input type="checkbox" name="object[]" value="', $value, '"><font style="margin-left: 40px;">', $value, '</font></label><br>';
}
}
?>
</p>
<input type="submit" name="button1" value="送信確認" />
</p>
</form>
</body>
</html>
■test13_output.php
<?php
session_start ();
if (isset ( $_POST ['button1'] )) {
$object = $_POST ['object'];
switch ($object) {
case "ゲーム" :
$object = "ゲーム";
break;
case "ショッピング" :
$object = "ショッピング";
break;
case "映画" :
$object = "映画";
break;
case "旅行" :
$object = "旅行";
break;
case "上記以外" :
$object = "上記以外";
break;
}
$_SESSION ['object'] = $object;
$err = array ();
mb_language ( "ja" );
mb_internal_encoding ( 'utf-8' );
if (count ( $_POST ['object'] ) == 0) {
$err ['object'] = 'お問い合わせ項目を選択してください。';
}
if (count ( $err ) >= 1) {
$_SESSION ['err'] = $err;
header ( 'Location:test13_input.php' );
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xm1ns="http://www.w3.org/1999/xhtml">
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>Contact</title>
<style type="text/css">
p#submit_btn {
text-align: center;
}
.content {
margin-left: 137px;
margin-top: 0px;
font-size: 95%;
font-weight: bold;
}
.form {
color: white;
background-color: #ad3140;
text-align: center;
/* padding-left: 370px; */
padding-top: 5px;
padding-bottom: 5px;
font-weight: bold;
font-size: 95%;
width: 825px;
}
.textform {
margin-left: 68px;
}
.textform_postalCode {
margin-left: 30px;
}
.textstyle {
font-size: 95%;
font-weight: bold;
}
.required {
font-size: 87%;
font-weight: normal;
color: red;
}
.textstyle1 {
margin-left: 98px;
}
# heading {
border-left: 10px solid #CC3366;
border-bottom: 1px dashed #CC3366; /* 下ボーダーを、1px幅の破線、線色#CC3366に */
}
</style>
</head>
<body>
<h1 id="heading">アンケート</h1>
<?php
function h($str) {
return nl2br ( htmlspecialchars ( $str, ENT_QUOTES, 'UTF-8' ), false );
}
?>
<p class="textstyle">
■お問い合わせ項目:</br><?php if (isset($_SESSION['object'])){ echo '<font color="black">'.implode("</br>", $_SESSION['object']).'</font>';}?></p>
</p>
<p></p>
</body>
</html>