开发者

(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. 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. The way to go about is to use the javascript's redirect.

<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"); in your javascript code. PHP just generate the page so it doesn't stop for waiting on a js function. Basically translate header("location:logout.php"); in js and you're ok.


You shouldn't store passwords in plain text.


Are you sure that works? You're calling header after outputting some text...

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)

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. (you could style the div like the messages that pop up here on SO at the top of the page).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜