这是我的HTML代码。
<HTML>
<head>STUDENT LOGIN</head>
<style>
.error { color: #FF0000; }
</style>
<body>
<p><span class ="error">* required field.</span></p>
<?php
class data
{
static $passwordErr="";
static $password="";
static $classError="";
static $Studentclass=0;
static $nameErr="";
static $Studentname="";
}
?>
<form method="POST" action=<?php echo ($_SERVER["PHP_SELF"]);?>>
NAME: <input type="text" name="NAME" ;?>
<span class="error">* <?php echo data::$nameErr ;?></span><br>
CLASS:<input type="text" name="CLASS" ?>
<span class="error">* <?php echo data::$classError ;?> </span>
<br>
PASSWORD:<input type="password" name="PASSWORD" ?>
<span class="error">* <?php echo data::$passwordErr ;?>
</span><br>
<input type="submit" name="LOGIN" >
<?php
if($_SERVER["REQUEST_METHOD"]=="POST") {
data::$Studentname=$_POST['NAME'];
if(empty($_POST['NAME'])) {
data::$nameErr="Name cannot be empty";
}
elseif (!preg_match("/^[a-zA-Z]*$/",data::$Studentname)) {
data::$nameErr="Name can contain only letters and whitespaces";
}
data::$Studentclass=0;
data::$Studentclass=filter_input(INPUT_POST,data::$Studentclass,FILTER_VALIDATE_ INT);
if(empty(data::$Studentclass)) {
data::$classError = "Class cannot be empty";
}
data::$password="";
data::$password=$_POST['PASSWORD'];
if(empty(data::$password)) {
data::$passwordErr="Password cannot be empty";
}
}
?>
</form>
</body>
</HTML>
因此,根据我编写的代码,如果输入空值或输入数字而不是名称等,它将显示相应的错误。 但是,当我运行此文件..它没有显示任何错误。 任何人都可以解释为什么会发生这种情况以及如何解决这个问题
答案 0 :(得分:0)
是因为脚本是执行的,所以根据你的脚本,我们看到:
<HTML>
<head>STUDENT LOGIN
</head>
<style>
.error {
color: #FF0000;
}
</style>
<body>
<p>
<span class ="error">* required field.
</span>
</p>
<?php
class data {
static $passwordErr="";
static $password="";
static $classError="";
static $Studentclass=0;
static $nameErr="";
static $Studentname="";
}
?>
<?php //!You are writting the form before you read the variables in your script ?>
<form method="POST" action=
<?php echo ($_SERVER["PHP_SELF"]);?>>
NAME:
<input type="text" name="NAME" ;?>
<span class="error">*
<?php echo data::$nameErr ;?>
</span>
<br>
CLASS:
<input type="text" name="CLASS" ?>
<span class="error">*
<?php echo data::$classError ;?>
</span>
<br>
PASSWORD:
<input type="password" name="PASSWORD" ?>
<span class="error">*
<?php echo data::$passwordErr ;?>
</span>
<br>
<input type="submit" name="LOGIN" >
<?php
//! In this part your form was written and you change the variables value, so you can not modify the form in this part
if($_SERVER["REQUEST_METHOD"]=="POST") {
data::$Studentname=$_POST['NAME'];
if(empty($_POST['NAME'])) {
data::$nameErr="Name cannot be empty";
}
elseif (!preg_match("/^[a-zA-Z]*$/",data::$Studentname)) {
data::$nameErr="Name can contain only letters and whitespaces";
}
data::$Studentclass=0;
data::$Studentclass=filter_input(INPUT_POST,data::$Studentclass,FILTER_VALIDATE_ INT);
if(empty(data::$Studentclass)) {
data::$classError = "Class cannot be empty";
}
data::$password="";
data::$password=$_POST['PASSWORD'];
if(empty(data::$password)) {
data::$passwordErr="Password cannot be empty";
}
}
?>
</form>
</body>
</HTML>
正确的方法是:
<HTML>
<head>STUDENT LOGIN
</head>
<style>
.error {
color: #FF0000;
}
</style>
<body>
<p>
<span class ="error">* required field.
</span>
</p>
<?php
class data {
static $passwordErr="";
static $password="";
static $classError="";
static $Studentclass=0;
static $nameErr="";
static $Studentname="";
}
?>
<?php
//! First, process your request variables!
if($_SERVER["REQUEST_METHOD"]=="POST") {
data::$Studentname=$_POST['NAME'];
if(empty($_POST['NAME'])) {
data::$nameErr="Name cannot be empty";
}
elseif (!preg_match("/^[a-zA-Z]*$/",data::$Studentname)) {
data::$nameErr="Name can contain only letters and whitespaces";
}
data::$Studentclass=0;
data::$Studentclass=filter_input(INPUT_POST,data::$Studentclass,FILTER_VALIDATE_ INT);
if(empty(data::$Studentclass)) {
data::$classError = "Class cannot be empty";
}
data::$password="";
data::$password=$_POST['PASSWORD'];
if(empty(data::$password)) {
data::$passwordErr="Password cannot be empty";
}
}
?>
<?php //! Then write your form ?>
<form method="POST" action=
<?php echo ($_SERVER["PHP_SELF"]);?>>
NAME:
<input type="text" name="NAME" ;?>
<span class="error">*
<?php echo data::$nameErr ;?>
</span>
<br>
CLASS:
<input type="text" name="CLASS" ?>
<span class="error">*
<?php echo data::$classError ;?>
</span>
<br>
PASSWORD:
<input type="password" name="PASSWORD" ?>
<span class="error">*
<?php echo data::$passwordErr ;?>
</span>
<br>
<input type="submit" name="LOGIN" >
</form>
</body>
</HTML>