简体   繁体   English

使用JQuery,Ajax和PHP将数据填充到表中

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

I want to populate a database result in an HTML table. 我想在HTML表中填充数据库结果。 When the <a class="editUsers"> is clicked a box should pop up to show data from an Ajax call. 单击<a class="editUsers">时,应弹出一个框以显示来自Ajax调用的数据。

This should be shown: 应该显示:

<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>';
  }
?>

Problem was fixed. 问题已解决。 I did a silly mistake and forgot to include a $ . 我犯了一个愚蠢的错误,忘了包含$ Thanks to Paul Roub for the answer, I quote: 感谢Paul Roub的回答,我引用:

For starters, ("#userInfo").html(response); 对于初学者, ("#userInfo").html(response); is missing a $ . 缺少$ Should be $("#userInfo").html(response); 应该是$("#userInfo").html(response); – Paul Roub 8 mins ago – Paul Roub 8分钟前

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM