我从 $sql 得到一个未定义的用户名,它应该是查询返回的数据。
$('#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代码:
<?php
session_start();
include 'anonconnect.php';
// username and password sent from form
$myusername= $_POST['selected'];
$sql = $dbh->prepare("SELECT * FROM Users WHERE UserLogin= :login");
$sql->execute(array(':login' => $myusername));
$user = $sql->fetch();
/*** close the database connection ***/
$dbh = null;
if($user->rowCount() == 1){
echo 1;
echo json_decode($user);
else {
echo 0;
}
?>
它可以很好地从选定的下拉列表中提取文本并传递它,但返回的函数找不到它。