doudi8829 2013-08-26 20:55
浏览 27
已采纳

试图将数组返回到表中的列表

Jquery - I am getting undefined but in the php is giving the response with my table information.

function formfill(dblist) {
    var tableheader = "<thead><tr><th>Database Nick</th><th>Database IP</th> <th></th></tr></thead>";

    $('#table2').empty();
    $('#table2').append(tableheader);

    $.each(dblist, function (key, value) {
        $('#table2').append('<tr><td>' 
        + dblist['DBNick'] 
        + '</td><td>' 
        + dblist['DBIP'] 
        + '</td><td><input class="add" id="add" name="add" value="add" type="submit"></input></td></tr>');

    }); 
}

$('#userlist').on('change', function () {
    var selected = $('#userlist').val();

    console.log(selected);

    $.ajax({
        url: '/php/user/userdbtable.php',
        type: 'POST',
        data: { user: selected },
        success: function (data) {
            formfill(data)
        },
        error: function (xhr, status, err) { console.log(xhr, status, err); }
    });
});

Here is the php it is sending out the correct data but i cant figure out how to pull it from the object.

<?php 
session_start(); 

include '../connect/anonconnect.php'; 

$myusername= $_POST['user'];

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

$userid = $useridsql['idUser'];

$query = 'SELECT * FROM DBList  INNER JOIN UserDBList 
    ON DBList.idDatabase = UserDBList.idDatabase ';//WHERE UserDBList.idUser = '.$userid; 
$sql = $dbh->prepare($query); 
$sql->bindParam(':username', $myusername, PDO::PARAM_STR, 12); 
$sql->execute(); 
$user = $sql->fetchAll(PDO::FETCH_ASSOC); 

if($sql->rowCount() == 1){ 
    header('Content-type: application/json'); 
    echo json_encode($user,JSON_FORCE_OBJECT); 
} 
else { 
    echo 0; 
}   
?>
  • 写回答

1条回答 默认 最新

  • douang1243 2013-08-26 21:11
    关注

    I think that your loop has an error. It should look like this:

    $.each(dblist, function (index, item) {
        $('#table2').append('<tr><td>' 
            + item['DBNick'] 
            + '</td><td>' 
            + item['DBIP'] 
            + '</td><td><input class="add" id="add" name="add" value="add" type="submit"></input></td></tr>');
    
    }); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵