0

This is my html code.

<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>

So according to the code i have written it will show respective errors if i enter an empty value or enter numbers instead of name and so on. But when i run this file..it does not show any error. Can anyone explain why this is happening and how i could fix it

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • You assign the messages *after* you display them. – Peter Apr 07 '18 at 14:27
  • Add [error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/845025#845025) to the top of your file(s) _while testing_ right after your opening PHP tag for example ` – RiggsFolly Apr 07 '18 at 14:28
  • HINT: `data::$Studentname=$_POST['NAME'];` should be `data::Studentname=$_POST['NAME'];` You dont have a `$Studentname` variable set ETC etc etc **But do add some error reporting** – RiggsFolly Apr 07 '18 at 14:29
  • If you are using HTML, you can use require tag to each obligatory input to save line code, before making a charge to the server – Neobugu Apr 07 '18 at 14:29
  • Suggest we close this as a TYPO – RiggsFolly Apr 07 '18 at 14:33
  • @RiggsFolly, `data::$Stundentname` is correct. `data::Studentname` would refer to a class constant. I don't think wrong code order qualifies as a typo. – Peter Apr 07 '18 at 14:48

1 Answers1

0

Is because the script is executed secuantial, so according with your script we see that:

<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>

The right way is:

<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>
Raul Cabrera A.
  • 169
  • 2
  • 6