title: jQuery.ajax POST 500 Internal Server Error on PHP script
body: <p>I am getting this error on a js function that sends an ajax request to a login script. here is the code:</p>
  
  <pre><code>function validateLogin(){
    $(document).ready(function(){
      $("#loginform").submit(function(){
        $.ajax({
          type: "POST",
          url: "./php/autologin.php",
          data: {
            'login': $("#login").val(), 
            'password': $("#password").val()
          },
          dataType: "json",
          success:function(data){
            if(data.status === "success"){
              alert(data.message);
              window.location = "./userarea.html?user=".concat(data.user);
            } else if (data.status === "error"){
              alert(data.message);
            }
          },
          error:function(thrownError){
            console.log(thrownError);
          }
        }); 
        return false;
      });
  
    });
  }
  </code></pre>
  
  <p>i am getting a POST 500 Internal Server Error on the url from the above code
  and here is what is being printed in the console of chrome:</p>
  
  <pre><code>POST http://localhost/capstone/atomos-4.0/php/autologin.php 500 (Internal Server Error) jquery-1.7.2.min.js:4
  f.support.ajax.f.ajaxTransport.send jquery-1.7.2.min.js:4
  f.extend.ajax jquery-1.7.2.min.js:4
  (anonymous function) validateLogin.js:4
  f.event.dispatch jquery-1.7.2.min.js:3
  f.event.add.h.handle.i jquery-1.7.2.min.js:3
  </code></pre>
  
  <p>I don't really understand what the post error is trying to tell me, can any one give me a nudge in the right direction to solving this. let me know if you need more info. thanks</p>
  
  <p>EDIT: here is the php function in question:
  <code>autologin.php</code>
      
  
  <pre><code>include './login.php';
  
  $login = $_POST['login'];
  $password = $_POST['password'];
  $loginattempt = json_decode(login($login, $password));
  
  if ($loginattempt-&gt;{'status'} === "success") {
    return json_encode(array('status' =&gt; "success", 'message' =&gt; "Login Successful!"));
    die();
  } else { 
    return json_encode(array('status' =&gt; "error", 'error' =&gt; "loginfailure", 'message' =&gt; ($loginattempt -&gt; {"message"})));
    die();
  }
  ?&gt;
  </code></pre>
  
  <p>and <code>login.php</code>:</p>
  
  <pre><code>&lt;?php
  
  include './connnect_to_mysql.php';
  
  function login($log, $pass) {
    $connection = json_decode(connect_to_mysql());
    if ($connection-&gt;{'status'} === "success") {
      $sqlquery = mysql_query("SELECT * FROM userdata WHERE login='$log' AND password='$pass'") or die(mysql_error());
      if (mysql_num_rows($sqlquery) == 1) {
        setcookie("user", $log, 60 * 60 * 24);
        return json_encode(array('status' =&gt; "success", 'message' =&gt; "Login Successful.", 'user' =&gt; $log));
        die();
      } else {
        return json_encode(array('status' =&gt; "error", 'error' =&gt; "loginfailure", 'message' =&gt; mysql_error()));
        die();
      }
    } else {
      return json_encode(array('status' =&gt; "error", 'error' =&gt; "connectionerror", 'message' =&gt; $connection -&gt; {'message'}));
      die();
    }
  }
  
  ?&gt;
  </code></pre>
  
  <p>EDIT 2:
  <code>connect_to_mysql.php</code>:</p>
  
  <pre><code>&lt;?php
  
  function connect_to_mysql() {
    $con = mysql_connect('localhost', 'user', 'pass') or die(mysql_error());
    if ($con) {
      $db = mysqli_select_db($con, 'db')or die(mysql_error());
      if ($db) {
        return json_encode(array('status' =&gt; "success", 'message' =&gt; "connected"));
        die();
      }
    } else {
      return json_encode(array('status' =&gt; "error", 'message' =&gt; mysql_error()));
      die();
    }
  }
  
  ?&gt;
  </code></pre>