簡體   English   中英

使用JQuery,Ajax和PHP將數據填充到表中

[英]Populating data into table using JQuery, Ajax and PHP

我想在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的回答,我引用:

對於初學者, ("#userInfo").html(response); 缺少$ 應該是$("#userInfo").html(response); – Paul Roub 8分鍾前

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM