1

I am using PHP and AJAX. I have login form and after entering login details and clicking on the submit button then my ajax not working. I am getting

500 Internal Server Error.

when I click on the submit button.

Screenshot of the console

enter image description here

Would you help me out with what I missed in this?

$(document).ready(function() {
  var isReqInprogress = false;
  $("#loginform").validate({
    rules: {
      username: {
        required: true
      },
      password: {
        required: true
      }
    },
    submitHandler: function(form) {
      //form.submit();
      if (isReqInprogress) {
        return;
      }
      isReqInprogress = true;
      $.ajax({
        url: "process.php",
        type: "POST",
        dataType: "json",
        data: $('#loginform').serialize(),
        success: function(data) {
          if (data.error == "true") {
            window.location = "list.php";
          }
          isReqInprogress = false;
        },
      }); // AJAX Get Jquery statment
    }
  });
});
<form method="post" id="loginform" action="" name="loginform">
  <input type="text" name="username" placeholder="USER NAME" class="input-field form-control box-shadow">
  <input type="password" name="password" placeholder="PASSWORD" class="input-field form-control box-shadow">
  <input type="hidden" name="action" value="loginform">
  <input type="submit" name="login" value="Login" class="w-btn w-btnbg-red" />
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/additional-methods.min.js"></script>

Process.php

<?php 

    ob_start();
    session_start();
    ini_set('display_errors', 1);
    require_once'connection.php';
    $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';

    switch ($action) {
        case 'loginform' : loginform($conn); break;
        default : header('Location: entry-form.php'); 
    }

    function loginform($conn){
        echo "working";
    }
?>
Sohail Ashraf
  • 10,078
  • 2
  • 26
  • 42
Naren Verma
  • 2,205
  • 5
  • 38
  • 95
  • 1
    You most probably have some error on server side. – Umair Khan Feb 01 '20 at 12:57
  • 1
    it's an server side error. your are showing here client side code. can your please show the server side code and also error details ?? – jual ahmed Feb 01 '20 at 12:57
  • 1
    Show us your PHP part, like `w...process.php`. The problem is there actually. – mitkosoft Feb 01 '20 at 13:03
  • Give me 5 mins I am updating it – Naren Verma Feb 01 '20 at 13:05
  • I updated the code. Please check it. – Naren Verma Feb 01 '20 at 13:07
  • Remove action="" in form and check for button submitted in php part instead of action, this is your answer I think https://stackoverflow.com/a/59594761/12232340 –  Feb 01 '20 at 13:13
  • Multiple problems: 1) for *development*, set error reporting so that all errors are shown. This gets rid of the 500 error. 2) Why are you buffering the output? Your script isn’t sending anything back; you’ve made it mute. 3) ajax response is expected to be json as evidenced by `if(data.error=="true")`. This requires `json_encode()`ing your response, and good practice is to issue header `application/json` 4) ajax won’t know what to do with redirected php; will the new page send back info for ajax call to process (if ajax can even receive it)? – Tim Morton Feb 01 '20 at 14:24
  • Need to comment all code of Process.php file and do below code to check if its working or not (mean post data of form) echo '
    '; print_r($_POST);exit;
    – Rohin Prajapati Feb 01 '20 at 13:21
  • Yes, I am getting the data now next? – Naren Verma Feb 01 '20 at 14:04
  • now add line by line code into your process.php file for checking which code giving your 500 error (dont add two line: ini_set('display_errors', 1); & ob_start(); into your code) – Rohin Prajapati Feb 01 '20 at 14:08
  • I am sure 500 error occur on require_once file or ob_start() – Rohin Prajapati Feb 01 '20 at 14:10
  • Let me check my process code. – Naren Verma Feb 01 '20 at 14:12

0 Answers0