说明
示例中使用用函数说明:
isset函数()
当要 判断一个变量是否已经声明的时候 可以使用 isset 函数,可以避免Notice:Undefined index...错误
htmlspecialchars() 函数
避免利用 $_SERVER["PHP_SELF"] 导致xss攻击,用户提交代码当做字符串处理
PHP 表单处理
test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<form action="test.php" method="post">
用户名:<input type="text" name="user" >
密码:<input type="password" name="passwd">
<input type="submit">
</form>
</body>
</html>
test.php
您输入的账户是:<?php echo $_POST["user"]; ?>!<br>
您输入的密码是:<?php echo $_POST["passwd"]; ?>
PHP 下拉菜单单选
<?php
$q = isset($_GET['q'])? htmlspecialchars($_GET['q']) : '';
if($q) {
if($q =='JavaScript') {
echo "您选择的是:JavaScript";
} else if($q =='python') {
echo "您选择的是:python";
} else if($q =='PHP') {
echo "您选择的是:PHP";
}
} else {
?>
<form action="" method="get">
<select name="q">
<option value="">选择语言:</option>
<option value="JavaScript">JavaScript</option>
<option value="python">python</option>
<option value="PHP">PHP</option>
</select>
<input type="submit" value="提交">
</form>
<?php
}
?>
PHP 下拉菜单多选
如果下拉菜单是多选的( multiple="multiple"),我们可以通过将设置 select name="q[]" 以数组的方式获取,以下使用 POST 方式提交
<?php
$q = isset($_POST['q'])? $_POST['q'] : '';
if(is_array($q)) {
$sites = array(
'python' => 'http://www.python.org',
'php' => 'https://www.php.net/',
'JavaScript' => 'https://www.javascript.com/',
);
foreach($q as $val) {
// PHP_EOL 为常量,用于换行
echo $sites[$val] . PHP_EOL;
}
} else {
?>
<form action="" method="post">
<select multiple="multiple" name="q[]">
<option value="">选择一种语言:</option>
<option value="python">python</option>
<option value="php">php</option>
<option value="JavaScript">JavaScript</option>
</select>
<input type="submit" value="提交">
</form>
<?php
}
?>
单选按钮表单
PHP 单选按钮表单中 name 属性的值是一致的,value 值是不同的
<?php
$q = isset($_GET['q'])? htmlspecialchars($_GET['q']) : '';
if($q) {
if($q =='JavaScript') {
echo "您选择的是:JavaScript";
} else if($q =='python') {
echo "您选择的是:python";
} else if($q =='PHP') {
echo "您选择的是:PHP";
}
} else {
?><form action="" method="get">
<input type="radio" name="q" value="python" />python
<input type="radio" name="q" value="JavaScript" />JavaScript
<input type="radio" name="q" value="PHP" />PHP
<input type="submit" value="提交">
</form>
<?php
}
?>
checkbox 复选框
<?php
$q = isset($_POST['q'])? $_POST['q'] : '';
if(is_array($q)) {
$sites = array(
'python' => 'http://www.python.org',
'php' => 'https://www.php.net/',
'JavaScript' => 'https://www.javascript.com/',
);
foreach($q as $val) {
// PHP_EOL 为常量,用于换行
echo $sites[$val] . PHP_EOL;
}
} else {
?><form action="" method="post">
<input type="checkbox" name="q[]" value="python"> python<br>
<input type="checkbox" name="q[]" value="php"> php<br>
<input type="checkbox" name="q[]" value="JavaScript"> JavaScript<br>
<input type="submit" value="提交">
</form>
<?php
}
?>
表单栅格化处理
利用htmlspecialchars()函数来对提交的数据进行栅格化处理,屏蔽提交代码渲染防止xss攻击
<?php
// 定义变量并默认设置为空值
$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)
{
if (!is_array($data)){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
}
return $data;
}
?>
表单综合示例
test.html
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
<body>
<h1>用户注册页面</h1>
<form method="post" action="test.php">
用户:<input type="text" name="user"><br>
密码:<input type="password" name="passwd"><br>
姓名:<input type="text" name="name"><br>
邮箱:<input type="email" name="email"><br>
性别:
<input type="radio" name="gender" value="true"/>男
<input type="radio" name="gender" value="false"/>女<br>
喜欢的开发语言:
<input type="checkbox" name="subjects[]" value="python"> python
<input type="checkbox" name="subjects[]" value="PHP "> PHP
<input type="checkbox" name="subjects[]" value="JavaScript"> JavaScript<br>
<input type="submit" value="提交">
</form>
</body>
</html>
test.php
<?php
// 定义变量并默认设置为空值
$user = $passwd = $name= $email = $gender = $subjects = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$user = test_input($_POST["user"]);
$passwd = test_input($_POST["passwd"]);
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$gender = test_input($_POST["gender"]);
$subjects = test_input($_POST["subjects"]);
print("所有数据:");
print_r($_POST);
print("<br>");
print("PHP获取到的【用户】:");
print($user);
print("<br>");
print("PHP获取到的【密码】:");
print($passwd);
print("<br>");
print("PHP获取到的【姓名】:");
print($name);
print("<br>");
print("PHP获取到的【邮箱】:");
print($email);
print("<br>");
print("PHP获取到的【性别】:");
print($gender);
print("<br>");
print("PHP获取到的【学科】:");
print_r($subjects);
}
function test_input($data)
{
if (!is_array($data)){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
}
return $data;
}
?>
结果展示
所有数据:Array ( [user] => xiaoming [passwd] => 123456 [name] => 小明 [email] => xiaoming@email.com [gender] => true [subjects] => Array ( [0] => python [1] => PHP ) )
PHP获取到的【用户】:xiaoming
PHP获取到的【密码】:123456
PHP获取到的【姓名】:小明
PHP获取到的【邮箱】:xiaoming@email.com
PHP获取到的【性别】:true
PHP获取到的【学科】:Array ( [0] => python [1] => PHP )