Thursday, 22 August 2013

how to do i get returned data from ajax call

how to do i get returned data from ajax call

I am getting a undefined on the username from $sql which should be the
returned data from the query.
$('#userlist').on('change', function () {
var selected = $("select option:selected").text();
console.log(selected);
// use ajax to run the check
$.ajax({
url: '/php/connect/userdropdowncheck.php',
type: 'JSON',
data: selected,
success: formfill,
error: function (xhr, status, err) { console.log(xhr, status, err); }
});
function formfill($sql) {
var username = $sql['UserLogin'];
var email = $sql['UserEmail'];
var admin = $sql['admin'];
var firstname = $sql['firstname'];
var lastname = $sql['lastname'];
var title = $sql['title'];
var company = $sql['company'];
console.log(username);
if (username.length > 0) {
console.log('Found user');
console.log(username);
$('#username').html($username);
}
else {
console.log('Failed to find user');
}
}
});
<?php
session_start();
include 'anonconnect.php';
// username and password sent from form
$myusername= $_POST['username'];
$sql = $dbh->prepare("SELECT * FROM Users WHERE UserLogin= :login");
$sql->execute(array(':login' => $myusername));
$sql = $sql->fetch();
/*** close the database connection ***/
$dbh = null;
if($sql->rowCount() == 1){
echo 1;
return $sql;
else {
echo 0;
}
?>
It is pulling the text from the selected drop down just fine and passing
it but the function on the return cannot find it.

No comments:

Post a Comment