POST sent by ajax is empty in php

Asked

Viewed 68 times

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.

1 answer

3


This is "typing error" in jQuery to inform the method nay we use method:, that is wrong:

$.ajax({
    method: "POST",

The correct is type::

$.ajax({
    type: "POST",

It is worth noting that in jQuery 1.9+ there is the "aliases" (nickname) for the type: that is method:, if it comes to updating your jQuery to the latest versions the method: will work

  • vlw, I’ll try here

  • 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.

  • @Victorgomez makes a var_dump first of all in php, so: var_dump($_POST);

  • I could see what the problem was, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.