title: Popup alert script in a php file
body: <p>I am trying to let a user log in. If the password and username is wrong, I want a popup to appear alerting the user on the error. When they close the alert, it goes back to index.php which is back to login screen. </p>
  
  <p>But when it is wrong password/username, ends up going back to index.php without any popup messages first. My browser setting is not blocking any popups. Can I know what I'm doing wrong please.  </p>
  
  <pre><code>&lt;?php
      if($login == true){
          //Do login process
          //this portion works as long as correct username and password
      }
      else{
          echo '&lt;script language="javascript"&gt;alert("Please enter valid username and password");&lt;/script&gt;';
          header("location:index.php");
      }
  ?&gt;
  
  
  //login.php
  
  &lt;?php
      $username = "exampleuser";
      $password = "examplepass";
      $host = "localhost";
  
      $dbHandle = mysql_connect($host, $username, $password) or die("Could not connect to database");
  
      $selected = mysql_select_db("database_name", $dbHandle);
  
      $myUserName = $_POST['user'];
      $myPassword = $_POST['pass'];
  
      if(ctype_alnum($myUserName) &amp;&amp; ctype_alnum($myPassword)){
          $query1 = "SELECT * FROM users WHERE username='$myUserName'";
  
          $result1 = mysql_query($query1);
          $count1 = mysql_num_rows($result1);
  
          if($count1 == 1){
              $query2 = "SELECT password FROM users WHERE username='$myUserName'";
              $result2 = mysql_query($query2);
  
              $row = mysql_fetch_array($result2, MYSQL_ASSOC);
              $pass = $row['password'];
  
              if(password_verify($myPassword, $pass)){
                  $seconds = 120 + time();
                  setcookie(loggedIn, date("F js - g:i a"), $seconds);
                  header("location:mysite.php");
              }
              else{
                  echo '&lt;script language="javascript"&gt;
                          alert("Please enter valid username and password");
                          window.location.href = "http://index.php";
                      &lt;/script&gt;';
                  die();
              }
          }
          else{
                  echo '&lt;script language="javascript"&gt;
                          alert("Please enter valid username and password");
                          window.location.href = "http://index.php";
                      &lt;/script&gt;';
                  die();
          }
      }
      else{
          echo '&lt;script language="javascript"&gt;
                  alert("Please enter valid username and password");
                  window.location.href = "http://index.php";
              &lt;/script&gt;';
          die();
      }
  ?&gt;
  </code></pre>