1
Hello, I am making a login system with ajax, which is activated with a button, however I am having problems, because the data sent by ajax, which are the username and password, They do not exist or are empty in PHP, I have tried to fix it in several ways I looked at several sites, and I can’t fix it. Someone could help me, I’m a couple of days like this, I’m still beginner. I thank you in advance.
HTML
<div id="login_inputs_top_menu">
<label id="login_inputs_label_top_menu">
Nome de usuario
</label>
<input type="text" id="input_user_top_menu">
<label id="login_inputs_label_top_menu">
Senha
</label>
<input type="password" id="input_password_top_menu">
</div>
<button id="submit_login_button">Login</button><br>
Jquery
$("#submit_login_button").click(function () {
var user_name = $("#input_user_top_menu").val();
var password = $("#input_password_top_menu").val();
$.ajax({
method: "POST",
url: "login.php",
data:{
user_name: user_name,
password: password
},
success: function (response) {
console.log(response.output);
}
});
});
PHP
if (isset($_SESSION)){
echo "Você ja fez login";
}else{
echo $_SERVER['REQUEST_METHOD'];
$userName = $_POST['user_name'];
$password = $_POST['password'];
}
With the debuger of Chrome I see that PHP, from what I asked him to inform, it always returns the method as GET, being that I asked POST, and returns that the POSTS that were sent are null.
vlw, I’ll try here
– Victor Gomez
Thanks, it worked, only that now has another problem, the part of the code that checks if the variables are empty, "if( Empty(..." It is still saying that they are empty, and if I echo the variables it the data appears.
– Victor Gomez
@Victorgomez makes a
var_dump
first of all in php, so:var_dump($_POST);
– Guilherme Nascimento
I could see what the problem was, thank you.
– Victor Gomez