<html>
<head>
<meta http-equiv="content-type" content="text/html" charset="utf-8">
<title>register form</title>
<style>
.error{
color:red;
}
</style>
</head>
<body>
<?php
if(isset($_POST['submitted'])){
/*
*通过filter_input()设置FILTER_SANITIZE_FILTER过滤可以去除html标记
*最后一个参数FILTER_FLAG_NO_ENCODE_QUOTES,它表示姓名里面的任何引号都不应该转换为相应的html实体
*/
$name = filter_input(INPUT_POST,'name',FILTER_SANITIZE_STRING,FILTER_FLAG_NO_ENCODE_QUOTES);
if($name){
echo "<p>Name:$name<br>\$_POST['name']:{$_POST['name']}</p>";
}else{
echo '<p class="error">Please enter your name</p>';
}
/*
*检验邮箱
*/
$email = filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL);
if($email){
echo "<p>Email Address: $email</p>" ;
}else{
echo '<p class="error">please enter your email address</p>';
}
/*
*检验是否为整数
*/
$icq = filter_input(INPUT_POST,'icq',FILTER_VALIDATE_INT);
if($icq){
echo "<p>icq number:$icq</p>";
}else{
echo '<p class="error">please enter your icq </p>';
}
/*
*去除html标签,包括里面的引号
*/
$comments = filter_input(INPUT_POST,'comments',FILTER_SANITIZE_STRING);
if($comments){
echo "<p>Name:$comments<br>\$_POST['comments']:{$_POST['comments']}</p>";
}else{
echo '<p class="error">please enter your email comments</p>';
}
}
?>
<form method="post" action="">
<fieldset>
<legend>注册表格</legend>
<p>Name:<input type="text" name="name"></p>
<p>Email Address:<input type="text" name="email"></p>
<p>ICQ num:<input type="text" name="icq"></p>
<p>Comments:<textarea name="comments" rows="5" cols="40"></textarea></p>
<input type="submit" value="提交" name="submit">
<input type="hidden" name="submitted" value="true">
</fieldset>
</form>
</body>
</html>
-
函数filter_has_var()可以查看一组变量里面是否包含特定名称的变量,在这个脚本里,我们可以使用如下代码判断表单是否被提交。
if(filter_has_var(INPUT_POST,'submitted')){
- filter_input_array()可以过滤变量数组