3
votes

I can't find a solution to this simple problem of accessing variable passed to my PHP script via AJAX. I have even tried isset($_POST) but it still fails to find the username and password variables.

Here is the AJAX call:

var u = $("#username", form).val();
var p = $("#password", form).val();

//testing
console.log('Username: '+u); // 'John'
console.log('Password: '+p); // 'test'

if(u !== '' && p!=='') {

    $.ajax({url: 'http://www.domain.net/php/user-login.php',
    data: {username:u,password:p},
        type: 'post',                   
    async: true,
    dataType:'json',
    beforeSend: function() {
        // This callback function will trigger before data is sent
        $.mobile.loading('show'); // This will show ajax spinner
    },
    complete: function() {
        // This callback function will trigger on data sent/received complete
        $.mobile.loading('hide'); // This will hide ajax spinner
    },
    success: function (data) {
        //save returned data to localStorage for manual button toggling later
        //window.localStorage.setItem('topGenderDataArray',data);
        console.log("Login successful: "+ data);
        console.dir(data);

        alert("Welcome back "+data['username']);
        $("#loginButton").removeAttr("disabled");
    },
    error: function (xhr,request,error) {
        // This callback function will trigger on unsuccessful action   
        alert('Network error has occurred please try again! '+xhr+ " | "+request+" | "+error);
    }
});

Here is the beginning of the PHP script:

if(isset($_POST['username']) && isset($_POST['password']))
{
    $data['username'] = $_POST['username'];
    $data['password'] = $_POST['password'];
}
else{
    $data['message']= "Sorry, an error occurred! []";
    $data['user_id']= -1;
    echo json_encode($data);
    exit();
}
3
What does this print: console.log("Login successful: "+ data);?hjpotter92
[object] the next line prints the object's properties...which are always "Sorry, an error occurred! []" and -1Zach
What does the network tab show in developer tools?jme11

3 Answers

6
votes

The problem is in your php code you do the echo json_encode($data) inside the else clause, while you should do it after the if and else just like this:

if(isset($_POST['username']) && isset($_POST['password']))
{
    $data['username'] = $_POST['username'];
    $data['password'] = $_POST['password'];
}
else
{
    $data['message']= "Sorry, an error occurred! []";
    $data['user_id']= -1;
}
echo json_encode($data);
1
votes

This contains your answer:

How to get body of a POST in php?

you are postin json data. User and password are not in the _POST array. They are in the request body. You need to json parse it

1
votes

You just need to print the $data after the if and else statement

Like this:

<?php
if(isset($_POST['username']) && isset($_POST['password']))
{
    $data['username'] = $_POST['username'];
    $data['password'] = $_POST['password'];
}
else
{
    $data['message']= "Sorry, an error occurred! []";
    $data['user_id']= -1;
}
echo json_encode($data);
exit();
?>

After is will work properly.