互联网程序设计·PHP语言之表单基础
第1关 文本类表单内容 – 资料填写
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label>
名字:<input type="text" name="name">
</label>
<label>
年龄:<input type="text" name="age">
</label>
<input type="submit" value="提交">
</form>
<?php
/********** Begin *********/
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
echo '欢迎'.$_POST['name'].',你今年'.$_POST['age'].'岁';
}
/********** End *********/
?>
</body>
</html>
第2关 按钮类表单内容 – 网页选择
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="get" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label>
<input type="radio" name="website" value="BAIDU" />百度
<input type="radio" name="website" value="GOOGLE" />Google
<input type="radio" name="website" value="TAOBAO" />淘宝
<input type="submit" value="提交">
</label>
</form>
<?php
/********** Begin *********/
$website = isset($_GET['website'])? $_GET['website'] : '';
if($website){
if($website == 'BAIDU'){
echo '百度 搜索'."<br>".'http://www.baidu.com';
}
else if($website == 'GOOGLE'){
echo 'Google 搜索'."<br>".'http://www.google.com';
}
else if($website =='TAOBAO'){
echo '淘宝'."<br>".'http://www.taobao.com';
}
}
/********** End *********/
?>
</body>
</html>
第3关 下拉菜单类表单内容 – 地址选择
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="get" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<select name="province">
<option value="">请选择一个省份:</option>
<option value="HUNAN">湖南</option>
<option value="GUANGDONG">广东</option>
</select>
<select name="city">
<option value="">请选择一个城市:</option>
<option value="CHANGSHA">长沙</option>
<option value="GUANGZHOU">广州</option>
</select>
<input type="submit" value="提交">
</form>
<?php
/********** Begin *********/
$province = isset($_GET['province'])? $_GET['province'] : '';
$city = isset($_GET['city'])? $_GET['city'] : '';
if( $province && $city){
if( $province== '湖南' && $ctiy=='长沙'){
echo "410000";
}else if( $province== '广东' && $ctiy=='广州'){
echo "510000";
}else{
echo "选定城市不在选定省份";
}
}
/********** End *********/
?>
</body>
</html>
第3关 下拉菜单类表单内容 – 地址选择
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="get" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<select name="province">
<option value="">请选择一个省份:</option>
<option value="HUNAN">湖南</option>
<option value="GUANGDONG">广东</option>
</select>
<select name="city">
<option value="">请选择一个城市:</option>
<option value="CHANGSHA">长沙</option>
<option value="GUANGZHOU">广州</option>
</select>
<input type="submit" value="提交">
</form>
<?php
/********** Begin *********/
$province = isset($_GET['province'])? $_GET['province'] : '';
$city = isset($_GET['city'])? $_GET['city'] : '';
if( $province && $city){
if( $province== '湖南' && $ctiy=='长沙'){
echo "410000";
}else if( $province== '广东' && $ctiy=='广州'){
echo "510000";
}else{
echo "选定城市不在选定省份";
}
}
/********** End *********/
?>
</body>
</html>
第4关 文件类表单内容 – 照片上传
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data">
<table class="reg">
<tr>
<td class="title" colspan="2">文件上传</td>
</tr>
<tr>
<th>照片文件:</th>
<td><input type="file" name="file" /></td>
</tr>
<tr><td colspan="2" class="td-btn">
<input type="submit" value="上传文件" class="button" />
<input type="reset" value="重新选择" class="button" />
</td></tr>
</table>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
/********** Begin *********/
//判断文件上传是否有错误
if( $_FILES['file']['error'] > 0 ){
die('上传文件有误!');
}
//输出文件信息
echo '文件的名称:' . $_FILES['file']['name'] . "<br>";
echo '文件的类型:' . $_FILES['file']['type'] . "<br>";
echo '文件的大小:' . $_FILES['file']['size'] /1024 . "Kb<br>";
echo '文件的临时路径:' . $_FILES['file']['tmp_name'] . "<br>";
/********** End *********/
//显示上传结果
if (move_uploaded_file($_FILES['file']['tmp_name'], $_FILES['file']['name'])) {
echo '上传成功!';
echo "<img src='" . $_FILES['file']['name'] . "'><br>";
} else {
die("上传错误!");
}
}
?>
</body>
</html>
1,137 Views