2015-10-21 38 views
1

我想在HTML表中填充数据库结果。单击<a class="editUsers">时,应弹出一个框以显示来自Ajax调用的数据。使用JQuery,Ajax和PHP将数据填充到表中

这应该被示出:

<table id="userInfo"> 
    <tr> 
     <thead> 
      <td>User</td> 
      <td>Mail</td> 
      <td>Admin Access</td> 
     </thead> 
    </tr> 
    <tr> 
     <td>Jane Doe</td> 
     <td>[email protected]</td> 
     <td>Yes</td> 
    </tr> 
</table>   

$(".editUsers").click(function(){ 
    $("#userInfo").fadeIn(1000); 
    $(".exitUsrMgmt").fadeIn(1000); //This is the close button for that popup 

    $.ajax({ //create an ajax request to load_page.php 
     type: "GET", 
     url: "includes/getUserData.php",    
     dataType: "html", //expect html to be returned     
     success: function(response){     
      ("#userInfo").html(response); 
     }, 
     error:function (xhr, ajaxOptions, thrownError){ 
      alert(thrownError); 
     } 
    }); 
}); 

<?php 
    include_once('config.php'); 

    //Create PDO Object 
    $con = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD); 
    //Set Error Handling for PDO 
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    //Query 
    $sql = "SELECT name, email, admin FROM user_admin"; 

    //Prepare Statement 
    $stmt = $con->prepare($sql); 
    $stmt->execute(); 

    while ($row = $stmt->fetch()){ 
    echo '<tr>'; 
    echo '<td>'.$row[0].'</td>'; 
    echo '<td>'.$row[1].'</td>'; 
    echo '<td>'.$row[2].'</td>'; 
    echo '</tr>'; 
    } 
?> 
+2

对于初学者,'(“#userInfo”)。html(response);'缺少'$'。应该是'$(“#userInfo”)。html(response);' –

+0

哇。我现在可以用砖块打自己。我看了30分钟的代码,并没有发现这个愚蠢的错误。现在一切正常。谢谢:) – nTuply

+0

这可以帮助你:http://stackoverflow.com/questions/30770664/jquery-click-not-working-on-appended-tr-from-ajax/30771297#30771297 –

回答

1

问题是固定的。我犯了一个愚蠢的错误,忘记了包括$。感谢保罗Roub的答案,我引述:

对于初学者来说,("#userInfo").html(response);缺少$。应该是 $("#userInfo").html(response); - Paul Roub 8分钟前

相关问题