title: Fade out a div that is presented when exception is thown in php
body: <p>I wish to have a div displaying an error message appear for a limited time after an exception is thrown in php. I've managed to do it when a form is submitted by using jQuery when checking if all fields are filled in:</p>
  <p>HTML:</p>
  <pre><code>    &lt;div id=&quot;error-message-box&quot;&gt;
  &lt;h1 class =&quot;error-headline&quot;&gt;Error!&lt;/h1&gt;
  &lt;p class =&quot;error-text&quot;&gt;&lt;?php $errorMessage ?&gt;&lt;/p&gt;
  </code></pre>
  
  <p>JS:</p>
  <pre><code>        $('form').submit(function () {
      var check = loginValidation($(this));
      if(check==false) {
          $(&quot;#error-message-box&quot;).fadeIn()
          $(&quot;.error-text&quot;).text(&quot;Fill in all the fields&quot;)
          $(&quot;#error-message-box&quot;).delay(7000).fadeOut();
      }
      return check;
  });
  </code></pre>
  <p>This works alright, but I cannot figure out how to do something similar when an exception is thrown in php. In my php-file I have this:</p>
  <pre><code>    function displayError ($eMessage) {
  $errorMessage = $eMessage;
  </code></pre>
  <p>}</p>
  <pre><code>    if(isset($_POST['log-in-btn'])) {
  $username=$_POST['username'];
  $password=$_POST['password'];
  try {
      $sql=&quot; SELECT * FROM User WHERE username ='&quot;.$username.&quot;' AND password= '&quot;.$password.&quot;' &quot;;
  $result = mysqli_query($conn, $sql);
  if(mysqli_num_rows($result)==1) {
      while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
      $_SESSION['username'] = $row[&quot;0&quot;];
      $_SESSION['password'] = $row[&quot;1&quot;];
      $_SESSION['name'] = $row[&quot;2&quot;];
      $_SESSION['surname'] = $row[&quot;3&quot;];
      $_SESSION['email'] =$row[&quot;4&quot;];
          }
  }
  else {
      throw new Exception(&quot;Username and password doesn't exsist&quot;);
  }
  }
  catch (Exception $e){
      displayError($e-&gt;getMessage());
  }
  </code></pre>
  <p>}</p>
  <p>The only way way I can think of at the moment is to have an echo of the HTML-code of my error-message-box in the displayError-function, but then I don't know how to have it visible for a limited amount of time. Or, maybe somehow add a trigger or listener in jQuery to check if the div is visible so its triggered when the HTML-code is echoed, however I how no idea if that works. Any tips?</p>