php提交表单校验例子






// 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); #把特殊字符转换为 HTML 实体
  return $data;
}
?>

PHP 验证实例

PHP 验证实例2

">
姓名:



电邮:



网址:



评论:



性别:
女性
男性




echo "

您的输入:

";
echo $name;
echo "
";
echo $email;
echo "
";
echo $website;
echo "
";
echo $comment;
echo "
";
echo $gender;
?>


 

来源参考: http://www.w3school.com.cn/php/php_form_validation.asp 里面还有为空出现提示语,校验输入的字符符合规定的方法

转载于:https://www.cnblogs.com/kaibindirver/p/10299175.html

你可能感兴趣的:(php提交表单校验例子)