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();
}
console.log("Login successful: "+ data);
? – hjpotter92