php學習 from表單-2
1.獲取from使用者輸入
PHP 中的 $_GET 和 $_POST 變數用於檢索表單中的資訊
例:
index.html
<html>
<head>
<meta charset="utf-8">
<title>from</title>
</head>
<body>
<form action="index1.php" method="post">
名字: <input type="text" name="fname">
年齡: <input type="text" name="age">
<input type="submit" value="提交">
</form>
</body>
</html>
index1.php
<?php
header("content-type:text/html;charset=GBK");
echo ("歡迎:".$_POST["fname"]);
echo("<br>");
echo ("你的年齡是:".$_POST["age"]."歲");
?>!
2.獲取select選擇框資料
表單使用 GET 方式獲取資料,action 屬性值為空表示提交到當前指令碼,我們可以通過 select 的 name 屬性獲取下拉選單的值:
例:
<?php
header("content-type:text/html;charset=GBK");
$q = isset($_GET['q'])? htmlspecialchars($_GET['q']) : '';
if($q) {
if($q =='BAIDU') {
echo '百度<br>http://www.baidu.com';
} else if($q =='GOOGLE') {
echo 'Google 搜尋<br>http://www.google.com';
} else if($q =='TAOBAO') {
echo '淘寶<br>http://www.taobao.com';
}
} else {
?>
<form action="" method="get">
<select name="q">
<option value="">選擇一個站點:</option>
<option value="BAIDU">百度</option>
<option value="GOOGLE">谷歌</option>
<option value="TAOBAO">淘寶</option>
</select>
<input type="submit" value="提交">
</form>
<?php
}
?>
htmlspecialchars() 函式把預定義的字元轉換為 HTML 實體。
預定義的字元是:
- & (和號)成為 &
- " (雙引號)成為 "
- ' (單引號)成為 '
- < (小於)成為 <
- > (大於)成為 >
3.獲取select多選框資料
如果下拉選單是多選的( multiple="multiple"),我們可以通過將設定 select name="q[]" 以陣列的方式獲取,以下使用 POST 方式提交,程式碼如下所示:
例:<?php
header("content-type:text/html;charset=GBK");
$q = isset($_POST['q'])? $_POST['q'] : '';
if(is_array($q)) {
$sites = array(
'BAIDU' => '菜鳥教程: http://www.baidu.com',
'GOOGLE' => 'Google 搜尋: http://www.google.com',
'TAOBAO' => '淘寶: http://www.taobao.com',
);
foreach($q as $val) {
// PHP_EOL 為常量,用於換行
echo $sites[$val] . PHP_EOL;
}
} else {
?>
<form action="" method="post">
<select multiple="multiple" name="q[]">
<option value="">選擇一個站點:</option>
<option value="BAIDU">百度</option>
<option value="GOOGLE">谷歌</option>
<option value="TAOBAO">淘寶</option>
</select>
<input type="submit" value="提交">
</form>
<?php
}
?>
3.獲取單選框資料
PHP 單選按鈕表單中 name 屬性的值是一致的,value 值是不同的,程式碼如下所示:
<?php
header("content-type:text/html;charset=GBK");
$q = isset($_GET['q'])? $_GET['q'] : '';
if($q) {
if($q =='baidu') {
echo '百度<br>http://www.baidu.com';
} else if($q =='GOOGLE') {
echo 'Google 搜尋<br>http://www.google.com';
} else if($q =='TAOBAO') {
echo '淘寶<br>http://www.taobao.com';
}
} else {
?><form action="" method="get">
<input type="radio" name="q" value="baidu" />Runoob
<input type="radio" name="q" value="GOOGLE" />Google
<input type="radio" name="q" value="TAOBAO" />Taobao
<input type="submit" value="提交">
</form>
<?php
}
?>
4.checkbox複選框
<?php
header("content-type:text/html;charset=GBK");
$q = isset($_POST['q'])? $_POST['q'] : '';
if(is_array($q)) {
$sites = array(
'BAIDU' => '百度: http://www.baidu.com',
'GOOGLE' => 'Google 搜尋: http://www.google.com',
'TAOBAO' => '淘寶: http://www.taobao.com',
);
foreach($q as $val) {
// PHP_EOL 為常量,用於換行
echo $sites[$val] . PHP_EOL;
}
} else {
?><form action="" method="post">
<input type="checkbox" name="q[]" value="BAIDU"> Runoob<br>
<input type="checkbox" name="q[]" value="GOOGLE"> Google<br>
<input type="checkbox" name="q[]" value="TAOBAO"> Taobao<br>
<input type="submit" value="提交">
</form>
<?php
}
?>
5.表單驗證
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>fromValidate</title>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
header("content-type:text/html;charset=GBK");
// 定義變數並預設設定為空值
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["name"]))
{
$nameErr = "請輸入姓名";
}
else
{
$name = test_input($_POST["name"]);
// 檢測名字是否只包含字母跟空格
if (!preg_match("/^[a-zA-Z ]*$/",$name))
{
$nameErr = "只允許字母和空格";
}
}
if (empty($_POST["email"]))
{
$emailErr = "請輸入郵箱";
}
else
{
$email = test_input($_POST["email"]);
// 檢測郵箱是否合法
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
$emailErr = "非法郵箱格式";
}
}
if (empty($_POST["website"]))
{
$website = "";
}
else
{
$website = test_input($_POST["website"]);
// 檢測 URL 地址是否合法
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website))
{
$websiteErr = "非法的 URL 的地址";
}
}
if (empty($_POST["comment"]))
{
$comment = "";
}
else
{
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["gender"]))
{
$genderErr = "請輸入性別";
}
else
{
$gender = test_input($_POST["gender"]);
}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>PHP 表單驗證</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
名字: <input type="text" name="name" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
網址: <input type="text" name="website" value="<?php echo $website;?>">
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
備註: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
<br><br>
性別:
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="girl">女
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="man">男
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>您輸入的內容是:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
</body>
</html>