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>';
}
?>
對於初學者,'(「#userInfo」)。html(response);'缺少'$'。應該是'$(「#userInfo」)。html(response);' –
哇。我現在可以用磚塊打自己。我看了30分鐘的代碼,並沒有發現這個愚蠢的錯誤。現在一切正常。謝謝:) – nTuply
這可以幫助你:http://stackoverflow.com/questions/30770664/jquery-click-not-working-on-appended-tr-from-ajax/30771297#30771297 –