2012-11-13 93 views
-1

我是新的PHP,但我已经做了一个注册脚本,工作正常。但问题是每次按下提交按钮来检查我的错误,我将进入一个新的页面。PHP错误显示

我的问题是我如何让错误出现在同一页上?

我用于html表单的代码。

我想错误显示在我犯的错误div框任何想法?

<div id="RegistrationFormLayout"> 
<h1>Registration Page</h1> 
     <div id="ErrorMessage"></div> 
      <form action="script/registration.php" method="post"> 
      <label for="Username">Username</label> 
      <input type="text" name="Regi_username"> 

      <label for="FirstName">FirstName</label> 
      <input type="text" name="Regi_Firstname"> 

      <label for="LastName">LastName</label> 
      <input type="text" name="Regi_Lastname"> 

      <label for="EamilAddress">Regi_EmailAddres</label> 
      <input type="text" name="Regi_EmailAddres"> 

      <label for="Password">Password</label> 
      <input type="password" name="Regi_password"> 

      <button type="submit" value="Submit" class="Login_button">Login</button> 
     </form>   
     </div> 
+1

使用会话错误信息存储为字符串,并显示在同一页上是否存在 – GBD

+1

当你点击提交表单,它重定向到'script/registration.php'因为你的'action =“script/registration.php”'。 – irrelephant

+0

表单在'registration.php'中? – itachi

回答

0

如果我理解正确,那么您希望表单验证错误在那里。这是一种非常常见的模式,简单的解决方案是始终将窗体的action属性设置为显示窗体的同一页面。这允许您在尝试显示表单之前执行表单处理(如果存在$ _POST值)。如果验证成功,则将重定向头发送到“下一步”页面(使用header())。

的基本模式是这样的(非常非常简单的PHP)

<?php 

if(count($_POST)) { 
    $errors = array(); 
    $username = trim($_POST['Regi_username']); 
    if(empty($username)) { 
    $errors[] = 'username is required'; 
    } 

    if(count($errors) == 0) { 
    header('Location: success.php'); 
    die(); 
    } 
} 
<ul class="errors"> 
    <?php foreach($errors as $error) { ?> 
    <li><?php echo $error;?></li> 
    <?php } ?> 
</ul> 
+0

Okey谢谢我会检查出来;) –

+0

我使用<?php include'script/registration.php'; ?>我在同一个registration.php中有表单,然后工作 –