0

I am unable to find a solution to this problem I have depicted below. It shows that the variables $fName and other variable like $MNane are undefined as well as other variable like $fnameerr and so on, even though I have defined them later in my php script.

I am trying to a validated registration form. I have never come across this issue as previously this code has worked perfectly many times before. I do not have much knowledge about this language.

HTML code:

   <!-- Header -->
  <header class="masthead bg-primary text-white text-center">
  <div class="container">
    <img class="img-fluid mb-5 d-block mx-auto" src="img/profile.png" 
  alt="">
    <h1 class="text-uppercase mb-0">SOCIETY-123</h1>
    <hr class="star-light">
    <h2 class="font-weight-light mb-0">Maintain your house on Rent - Sell 
  house - Manage the Society</h2>
   </div>
   </header>
<div>
   <form method="post" action="<?php echo htmlspecialchars($_SERVER[PHP_SELF]); ?>">  
    <center><h1 class="text-uppercase mb-0">Registration</h1></center>
    <hr class="star-dark mb-5">
    <table class="registration1">    
        <tr>
            <td style="padding-right: 60px;">Firstname:<input type="text" name="FName" placeholder=" Firstname" value="<?php echo $FName;?>"><span><?php echo $FNameerr;?></span></td> 
            <td style="padding-right: 60px;">Middlename:<input type="text" name="MName" placeholder=" Middlename" value="<?php echo $MName;?>"><span><?php echo $MNameerr;?></span></td>
            <td style="padding-right: 60px;">Lastname:<input type="text" name="LName" placeholder=" Lastname" value="<?php echo $LName;?>"><span><?php echo $LNameerr;?></span></td>
        </tr>
        <tr style="padding-top: 30px;">
            <td style="padding-top: 30px;">Gender: Male<input type="radio" name="Gender" <?php if(isset($Gender) && $Gender == "male") echo checked;?> value="male"></td>
            <td style="padding-top: 30px;">Female<input type="radio" name="Gender"<?php if(isset($Gender) && $Gender == "female") echo checked;?> value="female"></td>
            <td style="padding-top: 30px;">other<input type="radio" name="Gender"<?php if(isset($Gender) && $Gender == "other") echo checked;?> value="other"><span><?php echo $Gendererr;?></span></td>            
        </tr>
        <tr>
            <td style="padding-top: 30px;">Address: <input type="text" name="add" placeholder="Type your address here" size="30" value="<?php echo $add;?>"><span><?php echo $adderr;?></span></td>
            <td style="padding-top: 30px;">Occupation<input type="text" name="occu" placeholder=" occupation" value="<?php echo $occu;?>"><span><?php echo $occuerr;?></span></td>                
        </tr>
        <tr>
            <td style="padding-top: 30px;">Residence type: self owned<input type="radio" name="Resi"<?php if(isset($Resi) && $Resi == "selfowned") echo checked;?> value="selfowned"></td>
            <td style="padding-top: 30px;"> Rented<input type="radio" name="Resi"<?php if(isset($resi) && $Resi == "rented") echo checked;?> value="rented"><span><?php echo $Resierr;?></span></td>    
        </tr>
        <tr>
            <td style="padding-top: 30px;">E-mail id:<input type="text" name="eid" placeholder=" e-mail id" value="<?php echo $eid;?>"><span><?php echo $eiderr;?></span></td>
            <td style="padding-top: 30px;">password:<input type="password" name="pass" placeholder=" password" value="<?php echo $pass;?>"><span><?php echo $passerr;?></span></td>
        </tr>
        <tr>
            <td><input type="button" name="submit" value="submit" value="submit"></td>
        </tr>
    </table>
</form>

PHP CODE:

  <?php
  $FName = $MName = $LName = $Gender = $add = $occu = $Resi = $eid = $pass = 
"";
 $FNameerr = $MNameerr = $LNameerr = $Gendererr = $adderr = $occuerr = $Resierr = $eiderr = $passerr = "";

if ($_SERVER["REQUEST_METHOD"] == "POST"){
    if (empty($_POST["FName"])){
        $FNameerr = "firstname is required";
    }
    else{
        $FName = test_input($_POST["FName"]);
        if (!preg_match("/^[a-zA-z]*$/",$FName)){
            $FNameerr = "Only letter and whitespaces allowed";
        }
    }


    if (empty($_POST["MName"])){
        $MNameerr = "middlename is required";
    }
    else{
        $MName = test_input($_POST["MName"]);
        if (!preg_match("/^[a-zA-z]*$/",$MName)){
            $MNameerr = "Only letter and whitespaces allowed";
        }
    }


    if (empty($_POST["LName"])){
        $LNameerr = "lastname is required";
    }
    else{
        $LName = test_input($_POST["LName"]);
        if (!preg_match("/^[a-zA-z]*$/",$LName)){
            $LNameerr = "Only letter and whitespaces allowed";
        }
    }


    if (empty($_POST["Gender"])){
        $Gendererr = "choosing a gender is required";
    }
    else{
        $Gender = test_input($_POST["Gender"]);

    }


    if (empty($_POST["add"])){
        $adderr = "choosing a gender is required";
    }
    else{
        $add = test_input($_POST["add"]);

    }

if (empty($_POST["occu"])){
        $occuerr = "middlename is required";
    }
    else{
        $occu = test_input($_POST["occu"]);
        if (!preg_match("/^[a-zA-z]*$/",$occu)){
            $occuerr = "Only letter and whitespaces allowed";
        }
    }

  if (empty($_POST["Resi"])){
        $Resierr = "choosing a gender is required";
    }
    else{
        $Resi = test_input($_POST["Resi"]);

    }

 if (empty($_POST["eid"])){
     $eiderr = "email id is required";
    }
     else{
         $eid = test_input($_POST["eid"]);
    if (!filter_var($eid, FILTER_VALIDATE_EMAIL)) {
        $eidErr = "Invalid email format"; 
       } 
    }

    }

  function test_input($data){

   $data = trim($data);
  $data = stripslashes($data);
 $data = htmlspecialchars($data);
  return $data;


}    



?>


 here is my entire block of code

Though I have define the variables over here in the php script

halfer
  • 19,824
  • 17
  • 99
  • 186
nilesh
  • 1
  • 3
  • 1
    Is your php code executed before your HTML ? – Nick Parsons Nov 24 '18 at 12:51
  • 2
    Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Qirel Nov 24 '18 at 12:54
  • @NickParsons no – nilesh Nov 24 '18 at 12:59
  • @NickParsons do you have a solution??? – nilesh Nov 24 '18 at 13:02
  • You need to execute your php code before your HTML, because if it isn't executed before then your HTML (php within it) won't know what any of your variables are – Nick Parsons Nov 24 '18 at 13:03
  • The error pretty much sums it up: you are trying to use a variable, for example by doing `echo $FName;` before you have declared it, `$FName = ""; – Qirel Nov 24 '18 at 13:03
  • @NickParsons thank you your tip helped but now i have another issue that when i click on the submit button the validations dont work.any other tips??? – nilesh Nov 24 '18 at 13:14
  • @nilesh yes, you need to change your submit button to `` (notice the change in the type) and remove `action=""` – Nick Parsons Nov 24 '18 at 13:26

1 Answers1

0

You can try this, I hope it will work

<tr>
   <td style="padding-right: 60px;">Firstname:<input type="text" 
    name="FName" placeholder=" Firstname" value="<?= (!empty($FName)) ? $FName : "" ?>">
Shahriar Ahmad
  • 149
  • 1
  • 3