Skip to content
Advertisement

Javascript Alert in PHP file — trying to get it to redirect back to login page after alert notifies user of failed credentials

When I click on “ok” on the alert box after user credentials are submitted in error, it doesn’t redirect me back to the login page, instead it directs me to my authenticate.php file on my browser. How do I get it to direct back to the login page for the user to re enter their credentials? Below is my code, Thanks!

if ($stmt->num_rows > 0) {
$stmt->bind_result($id, $password);
$stmt->fetch();
// Account exists, now we verify the password.
// Note: remember to use password_hash in your registration file to store the hashed passwords.
if (password_verify($_POST['password'], $password)) {
    // Verification success! User has loggedin!
    // Create sessions so we know the user is logged in, they basically act like cookies but remember the data on the server.
    session_regenerate_id();
    $_SESSION['loggedin'] = TRUE;
    $_SESSION['name'] = $_POST['username'];
    $_SESSION['id'] = $id;
    header('Location: home.php');

// THIS CODE NEEDS TO BE EDITED TO DISPLAY POP UPS AND NOT GO TO NEW PAGE
} else {
    // Incorrect password
    echo '<script language="javascript">';
    echo 'alert("Incorrect Password")';
    echo '</script>';
} else {
// Incorrect username
echo '<script language="javascript">';
echo 'alert("Incorrect Username")';
echo '</script>';
}

$stmt->close();
}
?>

Advertisement

Answer

This code may help you,

echo ("<script language='JavaScript'>
    window.alert('Incorrect Password');
    window.location.href='http://someplace.com';
    </script>");
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement