search.php 文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
div {
border-style: double;
padding: 20px;
background-color: powderblue;
font-family: courier;
}
</style>
</head>
<body>
<form action="result.php" method="POST">
<div>
<span>請輸入姓名 : </span>
<!-- required 輸入框不得為空 -->
<input type="text" name="name" id="name" required><br><br>
<span>請選擇性別 : </span>
<input type="radio" name="gender" value="male" id="male">
<label for="male">男</label>
<input type="radio" name="gender" value="female" id="female">
<label for="female">女</label><br><br>
<label for="area">請選擇住址行政區 : </label>
<select name="adminis" id="area">
<option value="Taipei">台北市</option>
<option value="NewTaipei" selected>新北市</option>
<option value="Taoyuan">桃園市</option>
<option value="Taichung">台中市</option>
<option value="Kaohsiung">高雄市</option>
<option value="Tainan">台南市</option>
</select><br><br>
</div>
<br>
<input type="submit" value="送出">
</form>
</body>
</html>
result.php 文件
<?php
$name = $_POST["name"];
$gender = $_POST["gender"];
$adminis = $_POST["adminis"];
if($gender == "male"){
echo "嗨~ 住在".$adminis."的".$name."先生, 恭喜您抽中一台iPhone";
} else {
echo "嗨~ 住在".$adminis."的".$name."女士, 恭喜您抽中一台iPhone";
}
?>
如果把 PHP 語法插入 HTML 文件內,就可以把 data 存進陣列裡用 foreach 叫出來
search.php 文件
<?php
$adminis = array(
"Taipei" => "台北市",
"NewTaipei" => "新北市",
"Taoyuan" => "桃園市",
"Taichung" => "台中市",
"Kaohsiung" => "高雄市",
"Tainan" => "台南市"
);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
div {
border-style: double;
padding: 20px;
background-color: powderblue;
font-family: courier;
}
</style>
</head>
<body>
<form action="result.php" method="POST">
<div>
<span>請輸入姓名 : </span>
<!-- required 輸入框不得為空 -->
<input type="text" name="name" id="name" required><br><br>
<span>請選擇性別 : </span>
<input type="radio" name="gender" value="male" id="male">
<label for="male">男</label>
<input type="radio" name="gender" value="female" id="female">
<label for="female">女</label><br><br>
<label for="area">請選擇住址行政區 : </label>
<select name="adminis" id="area">
<?php foreach($adminis as $area_english => $area_chinese):?>
<option value="<?= $area_chinese?>"><?= $area_chinese?></option>
<?php endforeach;?>
</select><br><br>
</div>
<br>
<input type="submit" value="送出">
</form>
</body>
</html>
foreach():
和 endforeach;
搭配使用。<?php echo?>
可以寫成 <?= ?>
result.php 文件
<?php
$name = $_POST["name"];
$gender = $_POST["gender"];
$area = $_POST["adminis"];
if($gender == "male"){
echo "嗨~ 住在".$area."的".$name."先生, 恭喜您抽中一台iPhone";
} else {
echo "嗨~ 住在".$area."的".$name."女士, 恭喜您抽中一台iPhone";
}
?>