0

我从 $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;
}
?>

它可以很好地从选定的下拉列表中提取文本并传递它,但返回的函数找不到它。

4

2 回答 2

1

您的 jquery 代码可能是:

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');
    }
}

$('#userlist').on('change', function () {
var selected = $("select option:selected").text();
$.ajax({
    url: '/php/connect/userdropdowncheck.php',
    type: 'POST',
    data: {UserLogin:selected},
    success: function(data){
        formfill(data)
    },
    error: function (xhr, status, err) { console.log(xhr, status, err); }
});

});

和你的 php 代码:

<?php

session_start();
include 'anonconnect.php';

// username and password sent from form 
$myusername= $_POST['UserLogin'];  

$sql = $dbh->prepare("SELECT * FROM Users WHERE UserLogin= :login");
$sql->execute(array(':login' => $myusername));
$user = $sql->fetch();

/*** close the database connection ***/
$dbh = null;

if($sql->rowCount() == 1){
    //echo 1; //if you echo this it will cause that your jquery code will not be able to read the response as JSON and serialize it in array
     echo json_encode($user); // php array to json object
} else {
    echo 0;
}
?>
于 2013-08-23T03:13:35.387 回答
0

如果你使用JSON类型,你可以序列化你的 javascript 数组,然后传递给 php.ini。

前任:var jsonString = JSON.stringify(Array);

在 php 中,使用json_decode()json_encode()

前任:$data = json_encode($sql)

于 2013-08-23T03:30:02.043 回答