-2

These are my codes:

<?php
// Check connection
if ($conn->connect_error) {
    $error = "Wrong Username and Password!";
    $_COOKIE['err']=$error;
    echo '<script language="javascript">';
    echo 'alert("Wrong username or Password")';
    echo '</script>';
    header("Location: HomePage.php");
}else{
    echo "Connected successfully";
    $chckSelect ="SELECT DISTINCT intUsrType FROM trial1232016.tblUser WHERE strUsrName = '".$username."';";
    $result = $conn->query($chckSelect);
    if($result->num_rows > 0){
        echo "ends here!";
    }
?>

The question is: why does this code, do not generate the alert Wrong username or password. It just redirect to the HomePage.php which is the header...Why does it direct there in an instant without showing the wrong username or password? Can anyone help me?

2 Answers2

1

Echo is executed but before you can see it the line

header("Location: HomePage.php");

cause that everything that is echoed before is not seen as the page is redirected (reloaded again)

To see some change you can:

  1. Redirect to page with some

    header("Location: HomePage.php?invalid");
    

    Then in your view check if ?invalid property is set and then show message

  2. You can set some $_SESSION variable so that it is not visible in url
  3. You can implement some ajax login
0
// Check connection
    if ($conn->connect_error) {
        $error = "Wrong Username and Password!";
        $_SESSION['err']=$error;

        header("Location: HomePage.php");

    }else{
    echo "Connected successfully";
    $chckSelect ="SELECT DISTINCT intUsrType FROM trial1232016.tblUser WHERE strUsrName = '".$username."';";
    $result = $conn->query($chckSelect);
    if($result->num_rows > 0){
        echo "ends here!";
    }

then in your HomePage.php

 if ($_SESSION['err']){
    echo '<script language="javascript">';
    echo 'alert("Wrong username or Password")';
    echo '</script>';
}
Avihay m
  • 551
  • 1
  • 5
  • 22