使错误显示在表单上

时间:2013-10-22 19:49:59

标签: php

如何在表单顶部显示错误,以便在$ user-> success == true时,它不会显示我的表单。删除最后的其他内容会有所帮助,但随后会在成功后显示。一种方法是重定向。也许是tehre

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

2 个答案:

答案 0 :(得分:0)

在表单标记

之前添加此代码
 <?php if (isset($User->error) AND $User->error)?>
    <p><?php echo $User->error?></p>
 <?php?>

答案 1 :(得分: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>
<?
}
?>