使用php和ajax的Datatable.js

I have a php foreach code to output all the items from a database in the form of a table after clicking on a button which send ajax request to the php foreach code.But i want to use datatables.js in the table.I called the required files.But its not working.

For eg: This code works when a button is clicked(using ajax):

<?php
foreach($user in $users){
$output = "<div class='table-responsive'><table id='mytable' class='table table-striped'><tbody><tr><td>{$user}</td></tr></tbody></table></div>";
echo $output;
?>
<script>
$(document).ready(function(){
$('#mytable').dataTable();
}
</script>