I am attempting to build a simple Login page that utilizes jQuery Ajax using a database and table with saved username and password in a local development environment: XAMPP on Mac.
I will admit I'm a bit rusty when it comes to Ajax and perhaps that's the biggest issues so please be kind:
Everytime I click to submit Ajax request I get a jquery-3.6.0.min.js:2 POST http://localhost:8080/checkUser.php 500 (Internal Server Error) in the Chrome's Console. The checkUser.php is contained in the same folder as the HTML form.
I have tested my connection to the database via php so I feel the issue lies in the javascript for some reason not able to connect to the Ajax URL.
I appreciate whatever help you can offer. My apologies at the length of this code. I shall post my code:
Basic HTML Form
<div class="container">
<div id="div_login">
  <h1>Login</h1>
  <div id="message"></div>
  <div>
    <input type="text" class="textbox" id="txt_uname" name="txt_uname" placeholder="Username">
  </div>
  <div>
    <input type="password" class="textbox" id="txt_pwd" name="txt_pwd" placeholder="Password">
  </div>
  <div>
    <input type="button" value="Submit" name="but_submit" id="but_submit">
  </div>
</div>
My jQuery Ajax Call
$(document).ready(function() {
  $("#but_submit").click(function() {
    var username = $("#txt_uname").val().trim();
    var password = $("#txt_pwd").val().trim();
    if (username != "" && password != "") {
      $.ajax({
        url: 'checkUser.php',
        type: 'post',
        data: {
          username: username,
          password: password
        },
        success: function(response) {
          var msg = "";
          if (response == 1) {
            window.location = "home.php";
          } else {
            msg = "Invalid username and password!";
          }
          $("#message").html(msg);
        }
      });
    }
  });
});
My checkUser.php
<?php
session_start();
$host = "localhost";
$user = "root";
$password = "";
$dbname = "users";
$con = mysqli_connect($host, $user, $password, $dbname);
if (!$con) {
    die("Connection failed: " . mysqli_connect_error());
}
$uname = mysqli_real_escape_string($con, $_POST['username']);
$password = mysqli_real_escape_string($con, $_POST['password']);
if ($uname != "" && $password != "") {
    $sql_query = "select count(*) as cntUser from users where username='".$uname."' and password='".$password."'";
    $result = mysqli_query($con, $sql_query);
    $row = mysqli_fetch_array($result);
    $count = $row['cntUser'];
    if ($count > 0) {
        $_SESSION['uname'] = $uname;
        echo 1;
    } else {
        echo 0;
    }
}

