I'm having trouble getting an error message to output on the page I have already loaded. After the Log In button is pressed, I would like to check the input information, and if it is not correct return an error in a specific location without reloading the page. Is it possible to echo inside an element (such as inside my div as I am trying to do below)?
<?php
if (isset($_POST['login'])) { login(); }
function login(){
if(isset($_POST['username'])&&isset($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$password_hash=md5($password);
if(!empty($username)&&!empty($password)) {
//stuff
} else {
$loginerror = "You must supply a username and password";
}
}
}
?>
<div style="z-index: 10; float:right; margin-top: -20;">
<h3>Already a user? Log in:</h3>
<div><?php if (isset($loginerror)){echo $loginerror;} ?></div>
<form action="<?php echo $current_file; ?>" method="POST">
<div style="float: left; width: 80px;">Username:</div> <input type="text" name="username"> <br />
<div style="float: left; width: 80px;">Password:</div> <input type="password" name="password">
<input type="submit" value="Log In" name="login">
</form>
</div>
Because you are not supplying the form data to the login function, it can't verify anything. I was able to modify your code and make it smaller, but it doesn't utilize a login function.
<?php
$loginerror = "";
if(isset($_POST['login'])) {
if( !empty($_POST['username']) && !empty($_POST['password']) ) {
//stuff
}
else {
$loginerror = "You must supply a username and password.";
}
}
?>
<div style="z-index: 10; float:right; margin-top: -20;">
<h3>Already a user? Log in:</h3>
<div><?php echo $loginerror; ?></div>
<form action="" method="POST">
<div style="float: left; width: 80px;">Username:</div> <input type="text" name="username"> <br />
<div style="float: left; width: 80px;">Password:</div> <input type="password" name="password">
<input type="submit" value="Log In" name="login">
</form>
</div>
Now whenever you need to give the user a login error, simply use $loginerror = "error here";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.