I am trying to populate my table using the so-called modern method of updating parts of a site asynchronously using Ajax. So far I have managed to populate the table using pure php. Here is the code. Thank you in advance for your hints of a solution to this problem!
function showUsersBasic(){
$.ajax({
url: 'php_dbrequests\php_requests.php',
type:'POST',
dataType: 'json',
success: function(output_string){
$('#db-table-users').append(output_string);
}
});
Here is the code in 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);?>
I know I got the most basic information: my db connection is successful, the button calling the ajax function works. I double checked my link to the table. Please any hints would be greatly appreciated!
source
share