Php Ajax调用未填充表

Php Ajax调用未填充表,php,ajax,database,asynchronous,Php,Ajax,Database,Asynchronous,我试图使用所谓的现代方法来填充我的表,即使用Ajax异步更新网站的部分内容。到目前为止,我已经使用纯php填充了一个表。这是密码。提前感谢您的提示以解决此问题 function showUsersBasic(){ $.ajax({ url: 'php_dbrequests\php_requests.php', type:'POST', dataType: 'json', success: function(output_string

我试图使用所谓的现代方法来填充我的表,即使用Ajax异步更新网站的部分内容。到目前为止,我已经使用纯php填充了一个表。这是密码。提前感谢您的提示以解决此问题

function showUsersBasic(){
$.ajax({
        url: 'php_dbrequests\php_requests.php',
        type:'POST',
        dataType: 'json',
        success: function(output_string){
                $('#db-table-users').append(output_string);
            } // End of success function of ajax form
        }); // End of ajax call    }
以下是php_dbrequests\php_requests.php中的代码

<?php
require 'database.php';
$sql = "SELECT id, email, regdate FROM users";
$records = $conn->prepare( $sql );
$records->execute();
$results = $records->fetchAll( PDO::FETCH_ASSOC );
    $output_string = '';
    $output_string .=  '<table>';
    $output_string .= '<tr>';
    $output_string .= '<th>ID</th>';
    $output_string .= '<th>Email</th>';
    $output_string .= '<th>Register Date</th>';
    $output_string .= '</tr>';
foreach( $results as $row ){
    $output_string .=  "<tr><td>";
    $output_string .=  $row['id'];
    $output_string .=  "</td><td>";
    $output_string .=  $row['email'];
    $output_string .=  "</td><td>";
    $output_string .=  $row['regdate'];
    $output_string .=  "</td><td>";
    $output_string .=  "</td>";
    $output_string .=  "</tr>";
}
    $output_string .= '</table>';
echo json_encode($output_string);?>

我知道我已经了解了最基本的内容:我的db连接成功,调用ajax函数的按钮工作正常。我仔细检查了我的表。请,任何提示都将不胜感激

请使用
$.get()
函数使用ajax获取PHP数据

function showUsersBasic() {
    $.get("php_dbrequests\php_requests.php", function(output_string, status){
        $('#db-table-users').append(output_string);
    });
 }

这里出了什么问题?我们能了解更多的情况吗?仔细检查您的选择器(类别与id)

使用开发工具确保在ajax调用或添加
警报(响应)中得到响应到您的成功函数

*Doh!-尝试对响应发出警报。如果一切正常,您应该在这里看到php脚本的响应

请尝试以下代码:

success: function(response){
                $('#db-table-users').append(response.output_string);
            } // End of success function of ajax form
        }); // End of ajax call    }

我认为应该从ajax调用代码中删除参数“dataType”。您需要JSON,但它返回html。您还需要从php中的echo中删除
json_encode()

您需要修改php代码,以返回一个带有一些键的数组,如
数据
,并且不要返回
,因为html中已经有
。因此,只需返回带有填充数据的
tr

<?php
require 'database.php';
$sql = "SELECT id, email, regdate FROM users";
$records = $conn->prepare( $sql );
$records->execute();
$results = $records->fetchAll( PDO::FETCH_ASSOC );
    $output_string = '';
    #$output_string .=  '<table>';
    $output_string .= '<tr>';
    $output_string .= '<th>ID</th>';
    $output_string .= '<th>Email</th>';
    $output_string .= '<th>Register Date</th>';
    $output_string .= '</tr>';
foreach( $results as $row ){
    $output_string .=  "<tr><td>";
    $output_string .=  $row['id'];
    $output_string .=  "</td><td>";
    $output_string .=  $row['email'];
    $output_string .=  "</td><td>";
    $output_string .=  $row['regdate'];
    $output_string .=  "</td><td>";
    $output_string .=  "</td>";
    $output_string .=  "</tr>";
}
    #$output_string .= '</table>';
echo json_encode(array('data'=>$output_string) );?>

在chrome开发工具中,转到“网络”选项卡并进行ajax调用,它将显示是否返回预期结果或错误。请解释导致您发布此消息的意外行为?@WEBjuju点击“提交”按钮后,“我的表”的内容将不会填充。也不会返回任何错误消息。但是我已经验证了,至少按钮中对ajax函数的引用是有效的。HTML中是否有
id
属性与db表用户匹配的元素?@flauntster我没有安装chrome。我正在使用Mozilla Firefox来运行这个项目。你知道其中有什么同源选项吗?我用它替换了我的代码,得到了相同的结果。我用alert(output_string)替换了函数;我没有显示任何错误或操作。与其他建议一样,没有显示任何错误或操作。具有相同的无响应行为。我认为这是我所缺少的更大范围的东西(除了我明显缺乏专业知识之外)。我更新了ajax代码,你能在firebug extensions控制台中检查上面代码的响应吗?让我知道输出是什么。刚刚安装了插件。我不知道如何使用它。打开项目url后按f12。然后单击“控制台”选项卡,一旦您执行了基于ajax的操作,就会显示一个请求,单击它,您可以在特定选项卡中看到它的响应和其他详细信息
function showUsersBasic(){

            console.info('showUserBasic called');  

  $.ajax({
    url: 'php_dbrequests\php_requests.php',
    type:'POST',
    dataType: 'json',
    success: function(output_string){
            //OPEN FIREFOX FIREBUG EXTENSION & check console and click the ajax request to see its response
            console.log(output_string);           

            $('#db-table-users').append(output_string.data);
        } // End of success function of ajax form
    }); // End of ajax call   
 }