There are a few ways to do this.
Assign "Success" to $msg
and it will echo that on success, otherwise you echo $msg = 'Wrong code';
if it fails, along with checking if the POST array is not empty and everything set inside of it.
Plus, the way you have it now, would have thrown you a few notices about undefined variable/index and the POST array was located in the wrong spot along with brackets around 123
which shouldn't be there because you don't need them; it's just extra code for nothing, same for ($_POST['code'])
.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$msg = 'Success'; // or put it as empty, the choice is yours
if(!empty($_POST['code'])){
$pass = $_POST['code'];
if ($pass === "123") {
echo $msg;
} else {
echo $msg = 'Wrong code';
}
}
?>
<form autocomplete="off" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" target="_self" accept-charset="UTF-8">
<input type="password" name="code"/>
<input type="submit" name="submit">
</form>
Or you can do it like this and using isset()
for the $msg
variable:
$msg = '';
if(!empty($_POST['code'])){
$pass = $_POST['code'];
if ($pass === "123") {
$msg = 'Success';
} else {
$msg = 'Wrong code';
}
}
if(isset($msg)){
echo $msg;
}
I'm hoping that you're not planning on storing plain text passwords.
Use one of the following:
Important sidenote about column length:
If and when you do decide to use password_hash()
or the compatibility pack (if PHP < 5.5) https://github.com/ircmaxell/password_compat/, it is important to note that if your present password column's length is anything lower than 60, it will need to be changed to that (or higher). The manual suggests a length of 255.
You will need to ALTER your column's length and start over with a new hash in order for it to take effect. Otherwise, MySQL will fail silently.
Other links of interest: