<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数组并增加了边距和填充但是没有用。有什么建议?
答案 0 :(得分:0)
如果您正在使用jQuery
并且您有一个单独的html和php(MVC)
以error_container
<div id="error_container"></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>