Plan C
A single script with a single ajax call but the columns and rows are json_encoded separately this time. giving
Code...
if (isset($_GET['ajax'])) {
$mydata = [];
$cols = [];
$rows = [];
$data = $pdo->query('SELECT user_id as id
, firstname
, lastname
FROM user
LIMIT 3
');
$row = $data->fetch(PDO::FETCH_OBJ);
$keys = array_keys((array)$row);
foreach ($keys as $key) {
$cols[] = (object)[ 'name'=>$key, 'title'=>$key ];
}
do {
$rows[] = $row;
} while ($row = $data->fetch(PDO::FETCH_OBJ));
$mydata['columns'] = json_encode($cols);
$mydata['rows'] = json_encode($rows);
exit(json_encode($mydata));
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<meta http-equiv="content-language" content="en">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<!-- link to jquery functions -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type='text/javascript'>
$().ready( function() {
$("#get-data").click(function() {
$.get(
"",
{"ajax":1},
function(resp) {
$("#coldata").html(resp.columns)
$("#rowdata").html(resp.rows)
},
"JSON"
)
})
})
</script>
</head>
<body>
<span id="get-data" class="w3-button w3-blue w3-margin">Get Data</span>
<h3>Columns</h3>
<div id="coldata" class='data'></div>
<h3>Rows</h3>
<div id="rowdata" class='data'></div>
</body>
</html>