我想将数据库结果填充到 HTML 表中。当 <a class="editUsers">
单击后,会弹出一个框来显示来自 Ajax 调用的数据。
应该显示:
<table id="userInfo">
<tr>
<thead>
<td>User</td>
<td>Mail</td>
<td>Admin Access</td>
</thead>
</tr>
<tr>
<td>Jane Doe</td>
<td>janedoe@islost.com</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>';
}
?>
最佳答案
问题已解决。我犯了一个愚蠢的错误,忘记包含 $
。感谢 Paul Roub 的回答,我引用:
For starters,
("#userInfo").html(response);
is missing a$
. Should be$("#userInfo").html(response);
– Paul Roub 8 mins ago
关于javascript - 使用 JQuery、Ajax 和 PHP 将数据填充到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33264163/