I have some problem with my code. when I enter the correct username and password it fails to redirect to index.php.
i want to do php file side navigation instead of js side navigation using window.location.href
login.php:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' and isset($_POST['btn-login'])) {
$username = $_POST['username'];
$password = $_POST['password'];
if (User::authenticate($conn, $username, $password)) {
// Thực hiện sesison
Auth::login();
// redirect("index.php");
echo "success";
header("Location: index.php");
} else {
echo "<script>alert('Vui lòng đăng nhập lại');</script>";
}
}
?>
main.js:
$("form").on("submit", function (event) {
event.preventDefault(); // Prevent the form from being submitted normally
if (validateLoginForm()) {
// Use AJAX to submit the form
$.ajax({
url: "login.php", // URL to the PHP file that will handle the AJAX request
type: "POST",
data: $(this).serialize(), // Serialize form data for submission
success: function (response) {
// Handle success
if (response === "success") {
window.location.href = "login.php";
} else {
alert("Login failed. Please try again."); // Show error message
}
},
error: function (error) {
// Handle error
alert("An error occurred. Please try again."); // Show error message
},
});
}
});
Thank you for your help <3