0

如何在表单顶部显示错误,以便如果 $user->success == true,则不会显示我的表单。删除最后一个 else 会有所帮助,但成功后会显示表单。一种方法是重定向。也许是

if (isset($_POST["submit"]))
{
    if ($_POST["formid"] == $_SESSION["formid"])
    {
        $_SESSION["formid"] = '';
        $User->signin($_POST['username'], $_POST['password']);  
    }   
    else 
        $User->CheckUser();

        if ($User->success == true) {
            include ('in.php');                     
        }   

    if ($User->error)
        echo "<p>" . $User->error . "</p>";

    else 
        echo 'Don\'t process form';
        $_SESSION["formid"] = md5(rand(0,10000000));

} else {
?>
    <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
    Username:
    <input id="username" name="username" type="text" /><br />
    Password:
    <input id="password" name="password" type="password" /><br />
    <input type="hidden" name="formid" value="<?php echo $_SESSION["formid"]; ?>" />
    <input type="submit" name="submit" />
      <br />
  <a href="register.php">Register</a>
</form>
<?php }?>
4

2 回答 2

0

在您的表单标签之前添加此代码

 <?php if (isset($User->error) AND $User->error)?>
    <p><?php echo $User->error?></p>
 <?php?>
于 2013-10-22T19:53:02.423 回答
0

也许最简单的方法是创建一个变量$show_form来确定是否要显示表单,

$show_form = true;

if(isset($_POST['submit'])) {
   // do your form processing here.
   // If you decide everything is good and you don't want to show the form,
   // just add this line:
   $show_form = false;
} // don't use else here

if (true === $show_form) {
?>
    <form>...</form>
<?
}
?>
于 2013-10-22T19:55:04.227 回答