jQuery.ajax POST 500 Internal Server Error on PHP script

14,957

fix include './connnect_to_mysql.php'; to include './connect_to_mysql.php'; :)

Share:
14,957
Joe
Author by

Joe

Just a humble programmer.

Updated on June 04, 2022

Comments

  • Joe
    Joe almost 2 years

    I am getting this error on a js function that sends an ajax request to a login script. here is the 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;
        });
    
      });
    }
    

    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:

    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
    

    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

    EDIT: here is the php function in question: autologin.php

    include './login.php';
    
    $login = $_POST['login'];
    $password = $_POST['password'];
    $loginattempt = json_decode(login($login, $password));
    
    if ($loginattempt->{'status'} === "success") {
      return json_encode(array('status' => "success", 'message' => "Login Successful!"));
      die();
    } else { 
      return json_encode(array('status' => "error", 'error' => "loginfailure", 'message' => ($loginattempt -> {"message"})));
      die();
    }
    ?>
    

    and login.php:

    <?php
    
    include './connnect_to_mysql.php';
    
    function login($log, $pass) {
      $connection = json_decode(connect_to_mysql());
      if ($connection->{'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' => "success", 'message' => "Login Successful.", 'user' => $log));
          die();
        } else {
          return json_encode(array('status' => "error", 'error' => "loginfailure", 'message' => mysql_error()));
          die();
        }
      } else {
        return json_encode(array('status' => "error", 'error' => "connectionerror", 'message' => $connection -> {'message'}));
        die();
      }
    }
    
    ?>
    

    EDIT 2: connect_to_mysql.php:

    <?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' => "success", 'message' => "connected"));
          die();
        }
      } else {
        return json_encode(array('status' => "error", 'message' => mysql_error()));
        die();
      }
    }
    
    ?>