gpt4 book ai didi

php - 输入错误内联

转载 作者:行者123 更新时间:2023-12-03 08:54:27 26 4
gpt4 key购买 nike

This is my code for error function


function output_errors($errors) {
return '<ul><li>' . implode('</li><li>', $errors) . '</li></ul>';
}

This is my php code for login


include 'core/init.php';

if (empty($_POST) === false) {
$username = $_POST['username'];
$password = $_POST['password'];

if (empty($username) === true || empty($password) === true) {
$errors[] = 'Enter a valid username or password.';
}
elseif (user_exists($username) === false) {
$errors[] = 'User does not exist.';
}
elseif (user_active($username) === false) {
$errors[] = 'Please activate your account first.';
}
else {

if (strlen($password) > 32) {
$errors[] = 'Password too long. <br>';
}

$login = login($username, $password);

if ($login === false) {
$errors[] = 'Incorrect username or password.';
}
else {
$_SESSION['user_id'] = $login; //login returns user_id
header('Location: hashtagphotowall.php');
exit();
}
}

//visual representation of error arrays
//print_r($errors);

}
else {
$errors[] = 'No data received!';
}

if (empty($errors) === false) {
echo output_errors($errors);
}

This is my html code for login form


<div id="firstModal" class="reveal-modal" data-reveal aria-hidden="true" role="dialog">
<p class="modal-row"><em>Sign In.</em><hr></p>
<a href="#" class="button small">Connect with Facebook</a>
<p class="hr-enclosed">or login with email</p>

<form action="login.php" method="post">
<div class="field-box">
<input type="text" name="username" placeholder="Username:" />
<input type="password" name="password" placeholder="Password:" />
<input type="submit" name="loginbutton" class="button small" value="LOGIN" />
</div>
</form>

<p class="modal-row-sub">forgot your password? get help <a href="#">here</a>.<br>new user? sign up <a href="#" data-reveal-id="secondModal">here</a>.</p>
<a class="close-reveal-modal" aria-label="Close">&#215;</a>
</div>

The code is functioning but the error messages display on another webpage. I tried declaring a variable with no value and echo it inline with the input tag but I have no output. Maybe it's because I use the error arrays incorrectly. Any help on how can I make error arrays inline with the input tags in the login form? THANK YOU

最佳答案

我认为您希望将测试逻辑内置到与显示表单相同的login.php文件中。这样,它使将数据传回用户变得更加容易。

(伪代码)

Add a div to the top of the form: `<div id="errors">output_errors($errors) </div>` 
which tells user what to do. Obviously first time thru $errors = "" (or one space?)
The error div will be invisible to the user.

If previous submission made, test the data for validity.

If previous submission is made and input is good, go on to new page
(Success_thank_you_for_your_submission.php)

If previous submission is made, and input is not valid,
redisplay the page, only now the errors will be on top of the form.
redisplay with header("location:[login.php]");

You should take the time to re-populate all the fields the user originally submitted.
Sending back an empty form when there are errors won't make folks happy.

用户不断提交表单,直到正确为止。

关于php - 输入错误内联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31531211/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com