title: Jquery $.post returns object
body: <p>I am attempting to have the user enter their un/pw, click a button, have a javascript function take the un/pw entered, send it to a php script which will return a 1 or 0 based on whether the un/pw was valid.</p>
  
  <p>In the javascript page I have:</p>
  
  <pre><code>function handleLogin() {
      var username = document.getElementById('un').value;
      var password = document.getElementById('pw').value;
  
      var valid = $.post("getLogin.php", {"un": username, "pw": password}, "json");
      alert(valid);
  }
  </code></pre>
  
  <p>In the php file I have:</p>
  
  <pre><code>$username = $_POST['un'];
  $password = $_['pw'];
  $valid = 0;
  
  # This section will open a connection to the existing backup server and get the last ith_rid used.
  # It will then store that ith_rid to be used later and then close the database connection 
  $mysqlconn = new mysqli('localhost','username','password','database');
  if ($mysqlconn-&gt;connect_errno) {
      echo "Failed to connect to MySQL: (" . $mysqlconn-&gt;connect_errno . ") " . $mysqlconn-&gt;connect_error;
  }
  
  ###################################################
  ## Get username and password
  ##################################################  
  $res = $mysqlconn-&gt;query("SELECT username, password FROM table WHERE username = '" .$username . "'");
  if (!$res) {                                            ##If there is an error running query, display it to the screen.
      echo "Error: $mysqlconn-&gt;error \n";
  }
  
  while($row = $res-&gt;fetch_assoc()) {
      $un = trim($row["username"]);
      $pw = trim($row["password"]);
  }
  
  if ($un == $username &amp;&amp; $pw == $password){
      $valid = 1;
  }
  
  echo json_encode($valid);
  </code></pre>
  
  <p>The php does return something, but it is in an object. Not sure how to access the variable from the javascript in order to determine if it is 1 or 0.   </p>
  
  <p><strong>Edit:</strong></p>
  
  <p>So I changed things up a bit and it is working correctly now. </p>
  
  <p>carrierchange.js</p>
  
  <pre><code>jQuery(document).ready(function () {
      $("#content").append("&lt;form name='loginForm' autocomplete='off'&gt;");
      $("#content").append("&lt;table align=center&gt;");
      $("#content").append("&lt;tr&gt;&lt;td colspan=2 bgcolor=#87C9FF&gt;&lt;center&gt;&lt;h2&gt;Login&lt;/h2&gt;&lt;/center&gt;&lt;/td&gt;&lt;/tr&gt;");
      $("#content").append("&lt;tr&gt;&lt;td&gt;&lt;label for='un'&gt;Username:&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;input id='un' name='un'&gt;&lt;/td&gt;&lt;/tr&gt;");
      $("#content").append("&lt;tr&gt;&lt;td&gt;&lt;label for='pw'&gt;Password:&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;input id='pw' name='pw' type='password'&gt;&lt;/td&gt;&lt;/tr&gt;");
      $("#content").append("&lt;tr&gt;&lt;td colspan=2&gt;&lt;center&gt;&lt;input type='submit' class='btn' value='Login' onClick='handleLogin()'&gt;&lt;/center&gt;&lt;/td&gt;&lt;/tr&gt;");
      $("#content").append("&lt;/table&gt;");
      $("#content").append("&lt;/form&gt;");
      document.getElementById('un').focus().focus();
  });
  
  function handleLogin() {
      var username = document.getElementById('un').value;
      var password = document.getElementById('pw').value;
      var valid;
  
      $.get("getLogin.php", {un: username, pw: password}, "json", function(data) {
          console.log(data);
      }); 
  }
  </code></pre>
  
  <p>getLogin.php</p>
  
  <pre><code>&lt;?php
  
      $username = $_GET['un'];
      $password = $_GET['pw'];
      $valid = 0;
  
      # This section will open a connection to the existing backup server and get the last ith_rid used.
      # It will then store that ith_rid to be used later and then close the database connection 
      $mysqlconn = new mysqli('localhost','username','password','datebase');
      if ($mysqlconn-&gt;connect_errno) {
          echo "Failed to connect to MySQL: (" . $mysqlconn-&gt;connect_errno . ") " . $mysqlconn-&gt;connect_error;
      }
  
      ###################################################
      ## Get username and password
      ##################################################  
      $res = $mysqlconn-&gt;query("SELECT username, password FROM cc_user WHERE username = '" .$username . "'");
      if (!$res) {                                            ##If there is an error running query, display it to the screen.
          echo "Error: $mysqlconn-&gt;error \n";
      }
  
      while($row = $res-&gt;fetch_assoc()) {
          $dbusername = trim($row["username"]);
          $dbpassword = trim($row["password"]);
      }
  
      if ($dbusername == $username){
          $valid = 1;
      }
  
      echo json_encode($valid);
  ?&gt;
  </code></pre>