2013-10-22 91 views
0

如何在窗體頂部顯示錯誤,以便$ 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 }?> 

回答

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

你的表單標籤

<?php if (isset($User->error) AND $User->error)?> 
    <p><?php echo $User->error?></p> 
<?php?>