title: how can I AJAX form authentication?
body: <p>I have a login screen with username and password credentials that are validated throught a php script. once a user enters their username and password and hits submit. authenticate.php does an LDAP bind and if the user's credentials are legit they are redirected to say www.siteA.com, if the credentials are false an erorr is given saying "Invalid login, authentication failed. I would like to make this AJAX so when a user hits submit, right on that page it tells the user whether they entered valid or invalid login.</p>
  
  <p>Below is my code:</p>
  
  <p>For login form:</p>
  
  <pre><code>&lt;form action=authenticate.php method=post name=Auth class="appnitro"&gt;
      &lt;div class="form_description"&gt;
      &lt;h2&gt;Login&lt;/h2&gt;
      &lt;/div&gt;
  &lt;ul&gt;
          &lt;li id="li_1"&gt;
      &lt;label class="description" for="element_1"&gt;Username &lt;/label&gt;
          &lt;div&gt;
          input id="element_1" name="login" class="element text medium" type="text" maxlength="255" value=""/&gt;
          &lt;/div&gt;
      &lt;/li&gt;
          &lt;li id="li_2" &gt;
          &lt;label class="description" for="element_2"&gt;Password &lt;/label&gt;
          &lt;div&gt;
          &lt;input id="element_2" name="password" class="element text medium" type="password" maxlength="255" value=""/&gt;
          &lt;/div&gt;
      &lt;/li&gt;
          &lt;li class="buttons"&gt;
              &lt;input id="saveForm" class="button_text" type="submit" name="submit" value="Log In" /&gt;
      &lt;/li&gt;
      &lt;/ul&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>My LDAP Bind, Authenticate.php:</p>
  
  <pre><code>&lt;?php
      session_start();
  
      if( isset($_POST['login']) &amp;&amp; isset($_POST['password']) )
      {
          //LDAP stuff here.
          $username = trim($_POST['login']);
          $password = trim($_POST['password']);
  
          echo("Authenticating...");
          $ds = ldap_connect('ldap://ldap:port');
  
          ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
          ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
  
          //Can't connect to LDAP.
          if( !ds )
          {
              echo "Error in contacting the LDAP server -- contact ";
              echo "technical services!  (Debug 1)";
  
              exit;
          }
  
          //Connection made -- bind anonymously and get dn for username.
          $bind = @ldap_bind($ds);
  
          //Check to make sure we're bound.
          if( !bind )
          {
              echo "Anonymous bind to LDAP FAILED.  Contact Tech Services! (Debug 2)";
  
              exit;
          }
  
          $search = ldap_search($ds, "ou=People,DC=keler,DC=medioa,DC=com", "uid=$username");
  
          //Make sure only ONE result was returned -- if not, they might've thrown a * into the username.  Bad user!
          if( ldap_count_entries($ds,$search) != 1 )
          {
              echo "Error processing username -- please try to login again. (Debug 3)";
              redirect(_WEBROOT_ . "/try1b.php");
  
              exit;
          }
  
          $info = ldap_get_entries($ds, $search);
  
          //Now, try to rebind with their full dn and password.
          $bind = @ldap_bind($ds, $info[0][dn], $password);
          if( !$bind || !isset($bind))
          {
              echo "Login failed -- please try again. (Debug 4)";
              redirect(_WEBROOT_ . "/try1b.php");
  
              exit;
          }
  
          //Now verify the previous search using their credentials.
          $search = ldap_search($ds, "ou=People,DC=keler,DC=medioa,DC=com", "uid=$username");
          //if the user's login is legit then redirect to the siteA    
          $info = ldap_get_entries($ds, $search);
          if( $username == $info[0][uid][0] )
          {
              $_SESSION['username'] = $username;
              $_SESSION['fullname'] = $info[0][cn][0]; 
              header( "Location: www.siteA.com" );
  
              exit;
          }
          else
          {
              echo "Error. Access Denied";
              redirect(_WEBROOT_ . "/try1b.php");
  
              exit;
          }
          ldap_close($ds);
          exit;
      }
  ?&gt;
  </code></pre>