var handleownSubmitAjax = function(e) {
// Handle form submit
submitButton.addEventListener('click', function (e) {
// Prevent button default action
e.preventDefault();
// Validate form
validator.validate().then(function (status) {
if (status == 'Valid') {
// Hide loading indication
submitButton.removeAttribute('data-kt-indicator');
// Enable button
submitButton.disabled = false;
// Reads the user data from the form inputs
var email = $('#email').val();
var password = $('#password').val();
// Ajax request to match the user data with the database
$.ajax({
url: 'auth/authlogin.php', // path to the server page that checks the user data
method: 'POST',
data: {
email: email,
password: password
}, // user data sent to the server
success: function(response) {
// Successful response received from the server
if (response == 'success') {
const redirectUrl = form.getAttribute('data-kt-redirect-url');
email = "";
password = "";
if (redirectUrl) {
location.href = redirectUrl;
}
} else {
Swal.fire({
text: "Username and password are incorrect!",
icon: "error",
buttonsStyling: false,
confirmButtonText: "Understood",
customClass: {
confirmButton: "btn btn-primary"
}
});
}
},
error: function() {
// Error sending the ajax request
alert('Error sending Ajax request');
}
});
} else {
// Show error popup. For more info check the plugin's official documentation: https://sweetalert2.github.io/
Swal.fire({
text: "An error was detected while submitting the form. Please try again",
icon: "error",
buttonsStyling: false,
confirmButtonText: "Understood",
customClass: {
confirmButton: "btn btn-primary"
}
});
}
});
});
}
session_start();
$conn = new mysqli("localhost","root","","test");
if ($conn -> connect_errno) {
echo "Failed to connect to MySQL: " . $conn -> connect_error;
exit();
}
$email = $_POST['email'];
$password = $_POST['password'];
$query = "SELECT * FROM user WHERE email='$email' AND password='$password'";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) == 1) {
echo "success";
$_SESSION['email'] = $email;
}else {
echo "fail";
}
exit();
session_start();
if(!isset($_SESSION['email'])){
header('location: login.php');
}
general.js
you should point the ajax request to your server-side login scripts path.