title: PHP + Ajax Login
body: <p>Just having a few issues submitting a login form via ajax, I am primarily a PHP developer, I don't use Jquery + Ajax all that often with PHP.</p>
  
  <p>At the moment If i check the firebug POST data after the form has been submit it does appear to get the username and password that have been added to the form, however the page just reloads regardless of whether an incorrect username and password are added or if they are correct and no session is created.</p>
  
  <p>This is the form:</p>
  
  <pre><code>    &lt;form id="loginform" method="post"&gt;
      Username: &lt;input type="text" name="username" id="username" value=""&gt;
  
      Password: &lt;input type="password" name="password" id="password" value=""&gt;
  
      &lt;input type="submit" name="loginsub" id="loginsub" value="Login"&gt;
      &lt;/form&gt;
  </code></pre>
  
  <p>This is the Ajax/Jquery:</p>
  
  <pre><code>    &lt;script type="text/javascript"&gt;
      $(document).ready(function() {
  
      $('#loginform').submit(function() {
  
      $.ajax({
          type: "POST",
          url: '/class/login.php',
          data: {
              username: $("#username").val(),
              password: $("#password").val()
          },
          success: function(data)
          {
              if (data === 'Login') {
                  window.location.replace('/user-page.php');
              }
              else {
                  alert('Invalid Credentials');
              }
          }
      });
  
  });
  
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>And this is the PHP:</p>
  
  <pre><code>    class Users {
   public $username = null;
   public $password = null;
  
   public function __construct( $data = array() ) {
       if( isset( $data['username'] ) ) $this-&gt;username = stripslashes(        strip_tags( $data['username'] ) );
       if( isset( $data['password'] ) ) $this-&gt;password = stripslashes( strip_tags( $data['password'] ) );
   }
  
   public function storeFormValues( $params ) {
      $this-&gt;__construct( $params ); 
   }
  
   public function Login() {
       $success = false;
       try{
          $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); 
          $con-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
          $sql = "SELECT * FROM users WHERE username = :username AND password = :password LIMIT 1";
                      $user = username;
  
          $stmt = $con-&gt;prepare( $sql );
          $stmt-&gt;bindValue( "username", $this-&gt;username, PDO::PARAM_STR );
          $stmt-&gt;bindValue( "password", md5($this-&gt;password), PDO::PARAM_STR );
          $stmt-&gt;execute();
  
          $valid = $stmt-&gt;fetchColumn();
  
          if( $valid ) {
          $success = true;
                      session_start();
  
  
          session_regenerate_id();
          $_SESSION['user'] = $user['user'];
          session_write_close();
          echo ('Login');
          exit();
  
          }
  
          $con = null;
          return $success;
          }catch (PDOException $e) {
          echo $e-&gt;getMessage();
          return $success;
       }
  
   }
  </code></pre>
  
  <p>I guess it is not working because I am not calling the class and function, but I am not sure how to succesfully do so. I tried creating a controller page in between the 2 that would initiate the php class and function but to no avail.</p>
  
  <p>Just to edit, the login does work correctly if I remove the ajax and just call the php page via the login form action.</p>
  
  <p>Any ideas?</p>