Ajax call not populating table
You need to modify your php code to return an array with some key like data
and dont return table
as you already have a table
in your html. So just return tr
with populated data.
<?phprequire '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) );?>
Then modify your ajax code as :
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 }
Try this code:
success: function(response){ $('#db-table-users').append(response.output_string); } // End of success function of ajax form }); // End of ajax call }
I believe that you should remove the parameter "dataType" from you ajax call code. You are expecting JSON but it is returning html. You'll also need to remove the json_encode()
from the echo in the php.