简体   繁体   English

(PHP)如何在屏幕上保留JavaScript Alertbox

[英](PHP)how to hold javascript alertbox on screen

when user changes password I want to show message "Successfully changed!" 当用户更改密码时,我想显示消息“成功更改!”。 and when user clicks on OK button of alert box I call logout.php and force user to login with new password.But the problem is PHP header() is not waiting for alertbox and directly goes to logout.php. 当用户单击警报框的“ 确定”按钮时,我调用logout.php并强制用户使用新密码登录。但是问题是PHP header()没有等待警报框 ,而是直接转到logout.php。 my code- 我的代码-

if($count==1)
{
    $sqlchange="UPDATE $tbl_name SET password='$newpassword' WHERE userId='$myusername'";
    unset($result);
    $result=mysql_query($sqlchange,$link);
    if($result>0)
        { ?>
        <script type="text/javascript">
        alert("Your Password has been changed successfully.Please login again.");
        </script>
        <?php
        header("location:logout.php");
        exit;
        }
    else 
        {....

The reason why you are witnessing the header redirect is that PHP is a server-side language and its code executes before the javascript. 您目睹标头重定向的原因是PHP是一种服务器端语言,其代码 javascript 之前执行。 The way to go about is to use the javascript's redirect. 解决方法是使用javascript的重定向。

<script type="text/javascript">
  alert("Your Password has been changed successfully.Please login again.")
  document.location.href = 'logout.php';
</script>

Because you have to include the header("location:logout.php"); 因为您必须包含header("location:logout.php"); in your javascript code. 在您的JavaScript代码中。 PHP just generate the page so it doesn't stop for waiting on a js function. PHP仅生成页面,因此它不会停止等待js函数。 Basically translate header("location:logout.php"); 基本上翻译header("location:logout.php"); in js and you're ok. 在js中,你还好。

您不应以纯文本形式存储密码。

Are you sure that works? 您确定可行吗? You're calling header after outputting some text... 您在输出一些文本后调用header ...

Anyway a nice way to do it is: 无论如何,一个不错的方法是:

mysql_query(.....);
$_SESSION['message'] = "Password changed!";
header(....);

and then in the logout.php page (or wherever you redirect) 然后在logout.php页面中(或您重定向到的任何地方)

if (isset($_SESSION['message']))
    {
    echo "<div>".$_SESSION['message']."</div>";
    unset($_SESSION['message']);
    }

In this way you don't have to bother reconciliating server- and client-side calls and you avoid annoying JS popups. 这样,您就不必麻烦协调服务器端和客户端的调用,并且避免了烦人的JS弹出窗口。 (you could style the div like the messages that pop up here on SO at the top of the page). (您可以像在页面顶部SO上弹出的消息一样对div样式设置)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM