Does anyone know if there is a way of getting javascript to wait until I have pressed ok on an alert before it runs the next line of code?

The site I am developing is in PHP but I have made this function to bring up a message box:

function message($msg) {
        echo "<script language='javascript' type='text/javascript'>";
        echo "alert('".$msg."');";
        echo "</script>";
}

This is fine unless I change the header location in the next line in PHP as it automatically changes the page with showing the message.

Any suggestions?

Recommended Answers

All 3 Replies

This is fine unless I change the header location in the next line in PHP as it automatically changes the page with showing the message.

Can you post the code with that line in place please, so I can see exactly what is going on?

Airshow

if ($save=="new") { //New user
    $id=$_POST["username"];
    $password=generatePassword(8,8);
    $sql="INSERT INTO admin_users (username, password, email, accept_bookings, decline_bookings, remove_customers, amend_user, view_admin_log, view_error_log)
    VALUES('".$id."', '".$password."', '".$email."', '".$accept_bookings."', '".$decline_bookings."', '"
    .$remove_customers."', '".$amend_user."', '".$view_admin_log."', '".$view_error_log."');";
    $result = query_db($sql);
    // email user
    message("User created!, new password has been sent to user.");
    update_admin_log($user,"Users","User ".$id." created");
} else { //Amend user
    $id=$save;
    $sql="UPDATE admin_users 
    SET email='".$email."', accept_bookings='".$accept_bookings."', decline_bookings='".$decline_bookings."', remove_customers='"
    .$remove_customers."', amend_user='".$amend_user."', view_admin_log='".$view_admin_log."', view_error_log='".$view_error_log."' 
    WHERE username='".$id."';";
    $result = query_db($sql);
    // email user
    message("User details amended.");
    update_admin_log($user,"Users","User ".$save." details amend");
}
close_db();
header("location:admin_users.php");

Nathan,

In my experience, alerts suspend javascript execution in all browsers (though I have an old Javascritpt book that says otherwise). Therefore, I can't immediately see why the earlier alert should not suppress the later one.

message() is not native php. From what you say, it appears to write something into the served page that causes a Javascript alert.

Try "view source" in the browser to see the HTML/Javascript that is actually served.

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.