我正在处理一个表单,该表单将提交自己并显示已输入的值。我主要是关闭它,但是,我想检查用户是否错过了一个值,并告诉他们错过了哪个值。我无法弄清楚我会怎么做。代码如下。我知道这不安全,我现在只是写出一个想法,我稍后会修改以使其更安全。
<?php
$name = null;
$email = null;
$username = null;
$pwd = null;
$pwd2 = null;
if (!empty($_POST)){
ob_end_clean();
$name = htmlspecialchars($_POST["name"]);
$email = htmlspecialchars($_POST["email"]);
$username = htmlspecialchars($_POST["username"]);
$pwd = htmlspecialchars($_POST["pwd"]);
$pwd2 = htmlspecialchars($_POST["pwd2"]);
if ($pwd == $pwd2){
echo "
<!DOCTYPE html>
<html>
<head>
<title>Week 5 Assignment</title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'/>
</head>
<body>
<p>Welcome $name ! Please review the following information that you submitted:</p>
<p>You entered $email as your email.</p>
<p>You entered $username as your username.</p>
<p>You entered $pwd as your password.</p>
</body>
</html>";
}
else {
echo "
<!DOCTYPE html>
<html>
<head>
<title>Week 5 Assignment</title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'/>
</head>
<body>
<form action="; echo htmlspecialchars($_SERVER["PHP_SELF"]); echo" method=\"post\">
<p> Name: <input type=\"text\" name=\"name\" value=$name></p>
<p>Email: <input type=\"email\" name=\"email\" value=$email></p>
<p>Username: <input type=\"text\" name=\"username\" value=$username></p>
<p>Password: <input type=\"password\" name=\"pwd\" minlength = 4></p>
<p>Retype Password: <input type=\"password\" name=\"pwd2\"></p>
<input type=\"submit\">
</form>
<p>Passwords did not match</p>
</body>
</html>";
}
}
elseif (empty($_POST)){
echo "
<!DOCTYPE html>
<html>
<head>
<title>Week 5 Assignment</title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'/>
</head>
<body>
<form action="; echo htmlspecialchars($_SERVER["PHP_SELF"]); echo" method=\"post\">
<p> Name: <input type=\"text\" name=\"name\"></p>
<p>Email: <input type=\"email\" name=\"email\"></p>
<p>Username: <input type=\"text\" name=\"username\"></p>
<p>Password: <input type=\"password\" name=\"pwd\" minlength = 4></p>
<p>Retype Password: <input type=\"password\" name=\"pwd2\"></p>
<input type=\"submit\">
</form>
</body>
</html>";
}
?>
答案 0 :(得分:0)
foreach($_POST as $key => $value)
{
if(empty($value))
echo('The ' . $key . ' field is empty');
}