- 表单项目
- htmlspecialchars()函数用来将一些预定义的字符转成HTML实体
- 预定义字符 (结尾的分号都在中括号里的)
- &——>[&];
- ” ——>["];
- ’ ——>['];
- <——>[<];
- >——>[>];
- trim()函数用于去除用户输入数据中不必要的字符
- stripslashes()函数去除用户输入数据中的反斜杠[ \ ]
- $_SERVER[“REQUEST_METHOD”]通过判断是否等于POST来检测表单是否被提交
- > action = “$_SERVER[“PHP_SELF” 表示表单数据被发送到当前页面
- >”$_SERVER[“PHP_SELF”]”服务器变量可能被黑客利用,故应使用httpspecialchars()函数来防止被黑客利用。
Example:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Hello Php</title>
</head>
<body>
<?php
$user = $email = $sex = $csdn = $content = $checkbox = $school = "";
$checkboxArray = array("JAVA","Android");
if($_SERVER["REQUEST_METHOD"] == "POST"){
$user = input_special($_POST["user"]);
$email = input_special($_POST["email"]);
$sex = input_special($_POST["radio"]);
$csdn = input_special($_POST["CSDN"]);
$checkboxArray = $_POST["checkbox"];
$content = input_special($_POST["content"]);
$school = input_special($_POST["school"]);
}
function input_special($date){
$date = trim($date);
$date = stripcslashes($date);
$date = htmlspecialchars($date);
return $date;
}
?>
<div class="form" style="float:left;">
<form name = "form" method = "post" action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<p>
用户名:<input type = "text" name = "user"/>
</p>
<p>
用户邮箱:<input type = "text" name = "email"/>
</p>
<p>
性别:<input name = "radio" type = "radio" value = "男" checked = "true">男
<input name = "radio" type = "radio" value = "女">女
</P>
<p>
CSDN地址:<input type = "text" name = "CSDN"/>
</p>
<p>
简历:<textarea name = "content" rows = "5" cols = "25"></textarea>
</p>
<p>
学习语言:
<input name = "checkbox[]" type = "checkbox" value = "JAVA" checked="true"/>JAVA
<input name = "checkbox[]" type = "checkbox" value = "Android"/>Android
<input name = "checkbox[]" type = "checkbox" value = "C#"/>C#
<input name = "checkbox[]" type = "checkbox" value = "PHP"/>PHP
<input name = "checkbox[]" type = "checkbox" value = "HTML"/>HTML
</p>
<p>
毕业学校:
<select size = "1" name = "school">
<option value="泉州师范学院">泉州师范学院</option>
<option value="泉州大学">泉州大学</option>
</select>
</p>
<P>
<input name = "submit" type = "submit" value = "提交"/>
</p>
</form>
</div>
<div class="info" style="float:left;">
<?php
echo "<h3>提交的表单信息</h3>";
if($_SERVER["REQUEST_METHOD"] == "POST"){
echo "<br>";
echo $user;
echo "<br>";
echo $email;
echo "<br>";
echo $sex;
echo "<br>";
echo $csdn;
echo "<br>";
echo $content;
echo "<br>";
foreach ($checkboxArray as $value){
echo $value." ";
}
echo "<br>";
echo $school;
echo "<br>";
}
?>
<image src="EngineerZhong.jpg"/>
</div>
</body>
</html>
更新时间:2016年8月17日 By:EngineerZhong