注册重定向即使有验证错误

时间:2015-11-05 16:01:43

标签: php html

我创建了一个php注册页面。一切都运行良好。当我将文本框留空时显示错误。

然而,当我使用action方法重定向注册页面时,它总是重定向,无论是在我写东西时还是在我没有在文本框中输入内容时。

我希望当字段为空时页面不会重定向。我使用布尔值和break方法,但没有发生任何事情。

在这个问题上有人可以帮我吗?

<html>  
 <?php
 include 'header.php';

 $nameErr = $emailErr = $genderErr = $passwordErr = $addErr = $phoneErr = "";

 $fullname = $email = $gender = $password = $address = $phone = "";
$flag = false;
if ($_SERVER["REQUEST_METHOD"] == "POST") {

   if (empty($_POST["fullname"])) {
     $nameErr = "Name is required";
     break;
   $flag      
   } 
   else {
     $fullname = test_input($_POST["fullname"]);

   }


   if (empty($_POST["password"])) {
     $passwordErr = " password required";
  break;
   } 
   else {
     $password = test_input($_POST["password"]);
   }

   if (empty($_POST["email"])) {
     $emailErr = "Email is required";
   break;
   } 
   else {
     $email = test_input($_POST["email"]);
   }



   if (empty($_POST["address"])) {
     $addErr = "Address required";
   break;
   } 
   else {
     $gender = test_input($_POST["address"]);
   }

   if (empty($_POST["gender"])) {
     $genderErr = "Gender is required";
   break;
   } else {
     $gender = test_input($_POST["gender"]);
   }

   if (empty($_POST["phone"])) {
     $phoneErr = "phone no required";
   break;
   } else {
     $phone = test_input($_POST["phone"]);
   }
} 
?>
<body>
<br>
<div class="regis_div" >
  <form name="myForm"   <?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>  method="post" action="thanks.php" >

  <table class="table2" border="0" align="center" cellspacing="15" >
  <tr>
  <td colspan="3"><h1><center>User Registration Form</center></h1></td>
  </tr>
  <tr>
  <td width="291"> &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;Full name:</td>
    <td width="150"><input type="text"  name="fullname"></td>
    <td><span class="error"><font size="2" color="red">* <?php echo $nameErr;?></span></font></td>
</tr>
<tr>
  <td>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;Password</td>
    <td><input type="password" name="password"></td>
    <td><span class="error"><font size="2" color="red">* <?php echo $passwordErr;?></span></td>
    </tr>
<tr>
  <td>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;Confirm Password</td>
    <td><input type="password" name="Confirmpassword"></td>
    <td><span class="error"><font size="2" color="red">* <?php echo $passwordErr;?></span></td>
    </tr>
    <tr>
  <td>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;Email Address:</td>
    <td><input type="text" name="email" ></td>
    <td><span class="error"><font size="2" color="red">* <?php echo $emailErr;?></span></td>
    </tr>
    <tr>
    <td>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;Gender:</td>
    <td><input type="radio" name="gender" value="female">Female
        <input type="radio" name="gender" value="male">Male</td>
        <td><span class="error"><font size="2" color="red">* <?php echo $genderErr;?></span></td>
    </tr>
    <tr>
  <td height="80">&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;Address:</td>
    <td><textarea type="text" name="Address" class="address" rows="4" cols="20" ></textarea></td>

    <td><span class="error"><font size="2" color="red">* <?php echo $addErr;?></span></td>
    </tr>

    <tr>
  <td>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;Phone No:</td>
    <td><input type="text" name="phone"></td>
    <td><span class="error"><font size="2" color="red">* <?php echo $phoneErr;?></span></td>
    </tr>
    <tr>
    <td colspan="2"><input class="regbtn2" type="submit" value="Submit" align="center" ></td>
    </tr>
    </table>
    </form>
    </div>



</body>
</html>

1 个答案:

答案 0 :(得分:1)

if (empty($_POST["gender"])) {
    $genderErr = "Gender is required";
    break;
} else {
    $gender = test_input($_POST["gender"]);
}

if (empty($_POST["phone"])) {
    $phoneErr = "phone no required";
    break;
} else {
    $phone = test_input($_POST["phone"]);
}

http://perldoc.perl.org/perlretut.html#A-bit-of-magic:-executing-Perl-code-in-a-regular-expression只会突破forforeachwhiledo-whileswitch结构,而不是if语句。

它还不清楚您认为应该处理表单提交的脚本:

<form name="myForm" <?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>  method="post" action="thanks.php" >

如果您在上面发布的脚本名为form.php,则上述行将生成以下(无效)标记:

<form name="myForm" form.php method="post" action="thanks.php" >

提交后,执行将立即流向thanks.php,并且不会运行任何PHP错误检查(如上所述)。如果您希望运行上面发布的代码,则应将该表单标记更改为:

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">

然后,一旦您对提交没有错误感到满意,您就可以将用户重定向到新页面:

header('Location: thanks.php');

您还应注意,执行该行时,您将无法访问所有现有POST数据,因此如果您希望在重定向之前保留,则必须对其执行某些操作。标题。

但是,您的代码存在很多问题。

if (empty($_POST["fullname"])) {
    $nameErr = "Name is required";
    break;   // Already mentioned this line will have no effect
    $flag    // Why is this line here?  
} 

如果遇到任何错误,您如何知道使用当前结构?它必须如下所示:

if(isset($nameErr) || isset($passwordErr) || isset($emailErr) || ...)

您可以使用数组简化:

if (empty($_POST["fullname"])) {
    $errors['name'] = "Name is required";
    // ...
}

// ...

if(count($errors)) {
    // Some error occurred, don't redirect
} else {
    // ... do something with POST data ...
    header('Location: thanks.php');
    exit; // Always exit or die after issuing a header redirect
}

以及相关的标记:

<?php if(isset($errors['name'])) { ?>
<span class="error">
    <font size="2" color="red">* 
        <?php echo $errors['name']; ?>
    </font> <!-- Don't forget to close all of your opened HTML tags -->
</span>
<?php } ?>