【发布时间】:2017-04-14 07:45:22
【问题描述】:
我正在尝试使用所谓的现代方法来填充我的表,该方法使用 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);?>
我知道我已经掌握了最基本的内容:我的数据库连接成功,调用 ajax 函数的按钮有效。我仔细检查了对我表的引用。请,任何提示将不胜感激!
【问题讨论】:
-
在 chrome 开发工具中,转到网络选项卡并进行 ajax 调用,它将显示它是否返回预期结果或错误。
-
您能解释一下导致您发布此内容的意外行为吗?
-
@WEBjuju 点击提交按钮后,我的表的内容将不会填充。也没有返回错误信息。但我已经验证,至少在按钮中对 ajax 函数的引用是有效的。
-
HTML 中是否存在与 db-table-users 匹配的
id属性的元素? -
@flaunster 我没有安装 chrome。我正在使用 Mozilla Firefox 来运行该项目。你知道其中有什么同源选项吗?
标签: php ajax database asynchronous