title: Processing a jQuery UI dialog form in PHP
body: <p>I've been searching around on this for a couple of days, but haven't found any answers that fit my particular problem.  Some background:</p>
  
  <p>I am fairly new at PHP, and am attempting to create user log-in functionality on a practice site I set up using HTML, CSS and jQuery.  I have a form that is displayed by a jQuery UI dialog, with fields for username and password.  What I want to do is have the "Log In" button in the dialog submit the form to a separate file, "login.php", for validation and processing.  If the username exists in the MySQL database, I want it to return an error message that I can display in the same dialog.  Same goes for if the username exists, but the password is wrong.</p>
  
  <p>If the validation succeeds, I want the PHP file to set session variables, and redirect to index.php.</p>
  
  <p>This may be a simple error, or a number of them on my part, but I do not know which direction to go at this point.  Any assistance would be greatly appreciated.  The code is as follows (header.php):</p>
  
  <pre><code> &lt;script&gt;
      $(function() {
          var name = $("#userid"),
          password = $("#pw"),
          loginFields = $([]).add(name).add(password);
  
          $("#login_link").click(function() {
              $("#login_dialog").dialog("open"); 
          });
  
          $("#login_dialog").dialog({
              autoOpen: false,
              height: 200,
              width: 250,
              modal: true,
              buttons: {
                  Submit: function() {                            
                              $("#login_form").submit();
                          },
                  Cancel: function(){
                      $(this).dialog("close");
                  }                
              },
              close: function() {
                  loginFields.val("");
              }
          });
  
      });
  &lt;/script&gt;
      &lt;div id="banner"&gt;
          &lt;h1&gt;Header&lt;/h1&gt;
          &lt;span id="user"&gt;
              &lt;ul&gt;
                  &lt;?php
                      //if user is logged in, show name and Log Out option, else show Log In and Sign Up options
                      if(isset($_SESSION["user"])) {
                          echo "&lt;li&gt;Welcome " . $_SESSION["fname"] . "!&lt;li&gt;";
                          echo "&lt;li&gt;&lt;a href='#'&gt;Log Out&lt;/a&gt;&lt;/li&gt;";
                      } else {
                          echo "&lt;li&gt;&lt;a href='#' id='login_link'&gt;Log In&lt;/a&gt;&lt;/li&gt;";
                          echo "&lt;li&gt;&lt;a href='#'&gt;Sign up&lt;/a&gt;&lt;/li&gt;";
                      }
                  ?&gt;
              &lt;/ul&gt;
          &lt;/span&gt;
      &lt;/div&gt;
  
      &lt;nav id="mainnav"&gt;
          &lt;ul&gt;
              &lt;li&gt;&lt;a href="#"&gt;One&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="#"&gt;Two&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="#"&gt;Three&lt;/a&gt;&lt;/li&gt;
          &lt;/ul&gt;
      &lt;/nav&gt;
  
      &lt;div id="login_dialog" title="Current User"&gt;
          &lt;form id="login_form" action="login.php" method="post"&gt;
              &lt;fieldset&gt;
                  &lt;label for="userid"&gt;Username&lt;/label&gt;
                  &lt;input type="text" name="userid" id="userid" /&gt;
                  &lt;label for="pw"&gt;Password&lt;/label&gt;
                  &lt;input type="password" name="pw" id="pw" /&gt;
              &lt;/fieldset&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>The PHP, right now a collection of interacting functions, is as follows:</p>
  
  <pre><code>&lt;?php
  
  function connect() {
      $con = mysqli_connect("localhost", "site", "blargh");
  
      if (mysqli_connect_errno()) {
          alert("Failed to connect to MySQL: " . mysqli_connect_error());
      }
  
      return $con;
  }
  
  function getUser($username, $password) {
      $con = connect();
      $result = mysqli_query($con, "SELECT * FROM practice_user
                             WHERE username = " . $username . " AND password = " . $password);
  
      return $result;
  }
  
  function setSessionUser($userData) {
  
      $_SESSION["username"] = $userData["username"];
      $_SESSION["fname"] = $userData["first_name"];
      $_SESSION["lname"] = $userData["last_name"];
  }
  
  //validation functions
  
  function userExists($username, $password) {
      $con = connect();
  
      $result = getUser($username, $password);
  
      if (mysqli_num_rows($result) == 1) {
          return true;
      }
      else {
          return false;
      }
  }
  
  
  
  ?&gt;
  </code></pre>