如何在添加另一个元素时按下元素

时间:2014-12-30 02:38:58

标签: php jquery html css

<form id="password_form" action="" method="post">
    Username: </br>
    <input type="text" name="login_username"></br>
    Password:</br> 
    <input type="password" name="login_password">
    </br>
    <input type="submit" name="login" value="Log in">  
</form>

//form validation now occurs
<?php
    if (isset($_POST['login']))
    {
        $username = trim($_POST['login_username']);
        $password = trim($_POST['login_password']);

        //checks if correct credentials were given          
        $login = login($username, $password); 
        if ($login === FALSE)
        {
            $errors[] = 'That username/password combination is incorrect';
            $errors_1 = output_errors($errors);
            echo "<div id='errors'> $errors_1</div>";
        }
    } 
?>

以上是我的代码。用户提交表单后,我会检查以确保用户提供了正确的凭据。如果没有,则将错误消息添加到$ errors数组并输出。我希望错误输出在表单上方,并且表单按比例向下移动。我已经尝试在表单上方找到errors数组并增加了边距和填充但是没有用。有什么建议?

1 个答案:

答案 0 :(得分:0)

如果您正在使用jQuery并且您有一个单独的html和php(MVC)

error_container <div id="error_container"></div>

的形式定义div

然后从你的PHP回应响应。

$html = "<div id='errors'>$errors_1</div>";
echo json_encode($html);

jQuery ajax成功

success:function(result){
   $("#error_container").html(result);
}

如果你只使用PHP,你的php和html在同一页面

如果设置了$error_1,则在视图集中

,回显错误

//form validation now occurs
<?php
    if (isset($_POST['login']))
    {
        $username = trim($_POST['login_username']);
        $password = trim($_POST['login_password']);

        //checks if correct credentials were given          
        $login = login($username, $password); 
        if ($login === FALSE)
        {
            $errors[] = 'That username/password combination is incorrect';
            $errors_1 = output_errors($errors);
        }
    } 
?>

<!-- check is variable isset -->

<?php if(isset($errors_1)) { ?>

<div id='errors'><?php echo $errors_1; ?></div>

<?php } ?>

<form id="password_form" action="" method="post">
    Username: </br>
    <input type="text" name="login_username"></br>
    Password:</br> 
    <input type="password" name="login_password">
    </br>
    <input type="submit" name="login" value="Log in">  
</form>