tags:

views:

101

answers:

4

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 
        {....
+5  A: 

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>
Sarfraz
Just to clarify this: What you're experiencing is the fact that ALL php code effectively gets processed before ANY javascript does. The PHP happens on the server, then the page gets sent to the browser, and THEN the javascript gets processed. Hope that clears things up.
Bryan Ross
@Pekka: Yes if was not needed there, alert is modal dialog ofcourse and redirection will happen only after user clicks ok. Actually previously i was writing little differently but forgot to remove that `if` from there. Thanks anyways.
Sarfraz
nope its not working.alert box is coming but when i click on ok its not calling logout.phpif($result>0) { ?> <script type="text/javascript"> if (alert("Your Password has been changed successfully.Please login again.")) { document.location.href = 'logout.php'; } </script> <?php //exit; }
nectar
@Piyush you need to check what is actually being output in your browser. `$result > 0` is codswallop anyway, you need to use `mysql_num_rows($result) > 0`.
Pekka
yup its working.....no need to place alert inside if.thanks all
nectar
if user doen not clicks on **ok** and close the browser then logout.php would nt be called and sessions will not be deleted.???
nectar
@Piyush I think it is impossible to close a browser window when an alert box is open.
Pekka
+1  A: 

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.

dierre
+1  A: 

You shouldn't store passwords in plain text.

stagas
While technically a piece of good advice, that has absolutely nothing to do with the problem he's experiencing.
Bryan Ross
still, a *very* important piece of advice
nico
but better left as a comment
Bryan Ross
I wanted to post it as a comment but can't seem to find the comment button on the previous posts..
stagas
+1  A: 

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).

nico