【发布时间】:2015-10-21 16:07:54
【问题描述】:
我想在 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>';
}
?>
【问题讨论】:
-
对于初学者,
("#userInfo").html(response);缺少$。应该是$("#userInfo").html(response); -
哇。我现在可以用砖头打自己。我看了大约 30 分钟的代码,并没有发现那个愚蠢的错误。现在一切正常。谢谢:)
标签: javascript php jquery ajax pdo