当用户提交表单时,做到以下两件事是非常有必要:
使用 PHP trim() 函数去除用户输入数据中不必要的字符 (如:空格,tab,换行)。
使用PHP stripslashes()函数去除用户输入数据中的反斜杠 (\)
HTML>
// define variables and set to empty values
$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;
}
?>
PHP Form Validation Example
">
Name:
E-mail:
Website:
Comment:
Gender:
Female
Male
echo "
Your Input:
";echo $name;
echo "
";
echo $email;
echo "
";
echo $website;
echo "
";
echo $comment;
echo "
";
echo $gender;
?>