I'm sorry if this question has already been asked but none of the other answer to the other questions have helped me solve my problem.
I am writing a user login procedure that uses an .ajax() call to my login.php file to the log the user in. However, after several hours of debugging, I seem to have found out that the problem is that the data: {user: username, pass: password}line in my ajax call does not seem to send the data to my $_POST array in login.php. In fact, it won't even echo out my if statement in login.php. I know this problem sounds similar to others, but I have literally tried everything on this site that's related to the topic with no luck whatsoever.
Here's login_form.php. It contains the .ajax() code at the bottom:
<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
<link rel="stylesheet" type="text/css" href="css/custom-style.css" media="screen">
</head>
<body>
<div id="login_container">
<img src="img/sflogo.gif" alt="Sutton Ferneries" id="sflogo_login">
<p id="add_err">We're sorry, we weren't able to find that username/password.</p>
<input type="text" name="username" id="username" placeholder="Username">
<input type="password" name="password" id="password" placeholder="Password">
<p id="learn_login">Want to know the benefits of having an online account with us? <a href="#">Learn More.</a></p>
<button id="login_btn">Log In</button>
</div>
</body>
<script type="text/javascript">
$(document).ready(function() {
$("#add_err").css('display', 'none', 'important');
$("#login_btn").on("click", function(e) {
e.preventDefault();
var username = $("#username").val();
var password = document.getElementById("password");
password = password.value;
$.ajax({
url: "login.php",
data: { user: username, pass: password },
type: "POST",
dataType: "json",
beforeSend: function() {
$("#add_err").css('display', 'block', 'important');
$("#add_err").html("Loading...");
},
success: function(data) {
document.write(data);
if (data == 'true') {
$("#login_container").html(data);
//closeLightbox();
window.location = "index.php";
} else {
$("#add_err").css('display', 'block', 'important');
$("#add_err").html("Wrong username or password.");
}
}
});
});
});
</script>
</html>
And here's login.php:
<?php
header("Content-Type: application/json, true");
if (!isset($_POST)) {
echo 'Post is null';
} else {
echo 'Everything seems to be okay.';
}
$username = $_POST['user'];
$password = $_POST['pass'];
?>
dataType: "json",- you are expecting/trying to sendjsontype and data but echoing text/html from server. please try something likeecho json_encode('Post is null');success:call.