How to get error messages into the login modal
<p>I have a login modal that is within the header of my website and appears on screen when the user clicks on the login button. Whenever i submit the login form it creates a new page with the errors placed on it. Instead of this i am looking for the errors to come up within the login modal. Anyone know how i could achieve this ? </p>

<p>This is login.php</p>

<pre><code>&lt;?php                       //checks pword and email match on database
// Check if the form has been submitted:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

// Requires two additional files
require ('includes/login_functions.inc.php');
require ('C:\xampp\htdocs\final\includes\db.php');


// Check the login.
list ($check, $data) = check_login($dbc, $_POST['email'], $_POST['pass']);

if ($check) { // OK!

    // Set the session data.
    session_start();
    $_SESSION['id'] = $data['id'];
    $_SESSION['first_name'] = $data['first_name'];



    // Redirect the customer to loggedin page
    redirect_user('loggedin.php');

  } else { // If unsuccessful

        // Assign $data to $errors for login_page.inc.php:
    $errors = $data;

  }

   mysqli_close($dbc); // Close the database connection.

   }

    // Create the page:
     include ('includes/login_page.inc.php');
    ?&gt;
</code></pre>

<p>this is login_page.inc</p>

<pre><code>    &lt;link href="css/mystyle.css" rel="stylesheet"&gt;
&lt;?php   //displays errors and creates form

    // Include the header:
    $page_title = 'Login';
        if (isset($errors) &amp;&amp; !empty($errors)) {
                            echo '&lt;h1&gt;&lt;font color="orange"&gt;Error!&lt;/font&gt;          &lt;/h1&gt;
                            &lt;p class="error"&gt;The following error(s)  occurred:&lt;br /&gt;';
                            foreach ($errors as $msg) {
                        echo " - $msg&lt;br /&gt;\n";
                        }
                        echo '&lt;/p&gt;&lt;p&gt;Please try again.&lt;/p&gt;';
                        }


    // Display the form
     ?&gt;


        &lt;div class="modal fade" id="login-modal" tabindex="-1" role="dialog"   aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;"&gt;
          &lt;div class="modal-dialog"&gt;
            &lt;div class="loginmodal-container"&gt;
                &lt;h1&gt;Login to Your Account&lt;/h1&gt;&lt;br&gt;
              &lt;form action="login.php" method="post"&gt;
                &lt;input type="text" name="email" placeholder="Email Address"&gt;
                &lt;input type="password" name="pass" placeholder="Password"&gt;
                &lt;input type="submit" name="submit" class="login loginmodal-submit" value="Login"&gt;
              &lt;/form&gt;

            &lt;div class="login-help"&gt;
                    &lt;a&gt;Dont have an account? - &lt;/a&gt;&lt;a      href="register.php"&gt;Register&lt;/a&gt; 
                  &lt;/div&gt;
                  &lt;?php
                  // Display error messages if there are any
                    /*  if (isset($errors) &amp;&amp; !empty($errors)) {
                            echo '&lt;h1&gt;&lt;font color="orange"&gt;Error!&lt;/font&gt;    &lt;/h1&gt;
                            &lt;p class="error"&gt;The following error(s) occurred:&lt;br /&gt;';
                        foreach ($errors as $msg) {
                        echo " - $msg&lt;br /&gt;\n";
                        }
                        echo '&lt;/p&gt;&lt;p&gt;Please try again.&lt;/p&gt;';
                        }
               */
              ?&gt;
                &lt;/div&gt;
            &lt;/div&gt;
          &lt;/div&gt;
</code></pre>

<p>This is login_functions.inc</p>

<pre><code> &lt;?php                                  //checks for errors
function redirect_user ($page = 'index.php') {

    $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);

    $url = rtrim($url, '/\\');

    $url .= '/' . $page;

    header("Location: $url");
    exit(); // Quit the script.

    }


    // Checks the validation of the login.
    function check_login($dbc, $email = '', $password = '') {

    $errors = array(); // Initialize error array.

    // Validate the email address:
    if (empty($email)) {
        $errors[] = 'You forgot to enter your email address.';
    } else {
        $e = mysqli_real_escape_string($dbc, trim($email));
    }

    // Validate the password:
    if (empty($password)) {
        $errors[] = 'You forgot to enter your password.';
    } else {
        $p = mysqli_real_escape_string($dbc, trim($password));
    }

    if (empty($errors)) { // If everything's OK.

        // Retrieve the customer_id and first_name for that email/password combination:
        $q = "SELECT id, first_name FROM user WHERE email='$e' AND password=SHA1('$p')";        
        $r = @mysqli_query ($dbc, $q); // Run the query.

        // Check the result:
        if (mysqli_num_rows($r) == 1) {

            // Fetch the record:
            $row = mysqli_fetch_array ($r, MYSQLI_ASSOC);

            // Return true and the record:
            return array(true, $row);

        } else { // Not a match!
            $errors[] = 'The email address and password entered do not match those on file.';
        }

    } // End of empty($errors) IF.

    // Return false and the errors:
    return array(false, $errors);

    } // End of check_login() function.
</code></pre>