
示例
包含以下輸入字段: 必須與可選文本字段,單選按鈕,及提交按鈕:
首先是簡單的表單代碼:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
名字: <input type="text" name="name"><br/><br/>
E-mail: <input type="text" name="email"><br/><br/>
網(wǎng)址: <input type="text" name="website"><br/><br/>
備注: <textarea name="comment" rows="5" cols="40"></textarea><br/><br/>
性別:<input type="radio" name="gender" value="female">女
<input type="radio" name="gender" value="male">男
<br/><br/>
<input type="submit" name="submit" value="Submit">
</form>
注意:action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
這里是為了避免 CSS (Cross-Site Script) 跨站腳本攻擊。
當(dāng)用戶提交表單時(shí),我們將做以下兩件事情:
1、使用 PHP trim() 函數(shù)去除用戶輸入數(shù)據(jù)中不必要的字符 (如:空格,tab,換行)。
2、使用PHP stripslashes()函數(shù)去除用戶輸入數(shù)據(jù)中的反斜杠 ()
將這些過濾的函數(shù)寫在一個(gè)我們自己定義的函數(shù)中,這樣可以大大提高代碼的復(fù)用性,對(duì)上面的表單代碼進(jìn)行修改:
<html>
<body>
<?php
// 定義變量并默認(rèn)設(shè)置為空值
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$website = test_input($_POST["website"]);
$comment = test_input($_POST["comment"]);
$gender = test_input($_POST["gender"]);
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
名字: <input type="text" name="name"><br/><br/>
E-mail: <input type="text" name="email"><br/><br/>
網(wǎng)址: <input type="text" name="website"><br/><br/>
備注: <textarea name="comment" rows="5" cols="40"></textarea><br/><br/>
性別:<input type="radio" name="gender" value="female">女
<input type="radio" name="gender" value="male">男
<br/><br/>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>您輸入的內(nèi)容是:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
</body>
</html>
PHP - 必需字段
"名字", "E-mail", 和 "性別" 字段是必需的,各字段不能為空,網(wǎng)址可選, 如果存在,它必需包含一個(gè)有效的URL。
修改上面的代碼:
<html>
<body>
<?php
// 定義變量并默認(rèn)設(shè)置為空值
$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 (empty($_POST["email"])){
$emailErr="郵箱是必需的。";
}else{
$email = test_input($_POST["email"]);
}
if (empty($_POST["website"])){
$website = "";
}else{
$website = test_input($_POST["website"]);
}
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;
}
?>
<style>
.error
{ text-align:"center";color:red;}
</style>
<p><span class="error">* 必填字段。</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
名字: <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br/><br/>
E-mail: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br/><br/>
網(wǎng)址: <input type="text" name="website">
<span class="error"><?php echo $websiteErr;?></span>
<br/><br/>
備注: <textarea name="comment" rows="5" cols="40"></textarea><br/><br/>
性別:<input type="radio" name="gender" value="female">女
<input type="radio" name="gender" value="male">男
<span class="error">* <?php echo $genderErr;?></span>
<br/><br/>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>您輸入的內(nèi)容是:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
</body>
</html>