[英]Echo Success Message Once PHP
I am trying to create a "flash" success message which pops up when the user successfully changes their password.我正在尝试创建一条“闪现”成功消息,当用户成功更改其密码时会弹出该消息。 But, It doesn't work how I would like it to.但是,它不能像我希望的那样工作。
The basic idea is, when people enter their new password (and it passes to the database), it will echo to the page "Successfully updated password".基本思想是,当人们输入他们的新密码(并将其传递给数据库)时,它会回显到“成功更新密码”页面。 But it will only echo once (when the user refreshes, the echoed message will disappear and not display again until they submit a new password).但它只会回显一次(当用户刷新时,回显的消息将消失并且不会再次显示,直到他们提交新密码)。
I have tried searching around, but I can't seem to find any scripts that will actually work how I would like them to.我试过四处搜索,但我似乎无法找到任何可以按照我希望的方式实际工作的脚本。
This is my PHP function, currently:这是我的 PHP 函数,目前:
function updatePassword($conn, $newpwd, $username){
$newpwd = hash('md5', $newpwd);
mysqli_query($conn, "UPDATE users SET password = '$newpwd' WHERE username = '$username'");
}
Cheers.干杯。
I created something myself recently, the code could probably be better though, but it works.我最近自己创建了一些东西,虽然代码可能会更好,但它有效。
function flash_message($message, $type = 'success') {
switch($type) {
case 'success':
$class = "success";
break;
case 'info':
$class = "info";
break;
case 'error':
$class = "error";
break;
}
$_SESSION['flash_message'] = "<p class='flash_message ".$class."'>".$message."</p>";
}
function show_flash_message() {
if (isset($_SESSION['flash_message'])) {
$message = $_SESSION['flash_message'];
unset($_SESSION['flash_message']);
return $message;
}
return NULL;
}
You use show_flash_message()
on the page where you want to display it.您可以在要显示它的页面上使用show_flash_message()
。 If there is no message, it wont display anything.如果没有消息,它将不会显示任何内容。
You'd call it by doing this:你可以通过这样做来调用它:
function updatePassword($conn, $newpwd, $username){
$newpwd = hash('md5', $newpwd);
mysqli_query($conn, "UPDATE users SET password = '$newpwd' WHERE username = '$username'");
flash_message('Successfully changed your password');
}
The different classes are for if you want to change the display of the message.如果要更改消息的显示,则使用不同的类。 (Wrong username/password is an error, e-mail been sent can be info/success etc.) (错误的用户名/密码是错误,发送的电子邮件可以是信息/成功等)
Let me explain you pseudo logic.让我解释一下伪逻辑。
Steps:脚步:
1) When your password change is done successfully, assign success message to a session variable. 1) 成功更改密码后,将成功消息分配给会话变量。
$_SESSION['message'] = 'Password changed successfully.';
2) On the redirected success page, echo this. 2)在重定向成功页面上,回显这个。
if (isset($_SESSION['message'])) {
echo $_SESSION['message'];
unset($_SESSION['message']);
}
Also, unset()
it, so that, it will not be shown other time again.此外, unset()
它,这样,它就不会在其他时间再次显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.