如何解决 PHP 中的"未定义变量"问题?



我找不到下面描述的这个问题的解决方案。它显示变量$fName和其他变量$MNane以及其他变量$fnameerr等都是未定义的,尽管我稍后在php脚本中定义了它们。

我正在尝试一个经过验证的注册表。我从来没有遇到过这个问题,因为以前这个代码已经完美地工作了很多次。我对这种语言了解不多。

HTML代码:

<!-- 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代码:

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

尽管我已经在php脚本中定义了变量

你可以试试这个,我希望它能起作用

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

最新更新