This is a .php file that creates a table.
<table id="contact-messages">
<thead>
<tr>
<th>Username</th><th>Category</th><th>Message</th><th>Created at</th>
</tr>
</thead>
<tbody>
<?php
foreach ($contact_messages as $message) {
echo '<tr>'
. '<td>' . htmlentities($message['username']) . '</td>'
. '<td>' . htmlentities(ucfirst($message['category'])) . '</td>'
. '<td>' . nl2br(htmlentities($message['message'])) . '</td>'
. '<td class="created-at" data-created_at="' . htmlentities($message['created_at']) . '"></td>'
. '</tr>';
}
?>
</tbody>
</table>
And this is the .js file for that page. This code changes the content of the table.
$.get("contact-messages.php", { "category": category }, function (data) {
$("#contact-messages").find("tbody").empty(); // Empty the old messages.
for (var i = 0; i < data.length; i++) {
$("#contact-messages").find("tbody")
.append(($("<tr/>")
.append($("<td/>", { text: ((data[i].username === null) ? '' : data[i].username) }))
.append($("<td/>", { text: data[i].category }))
.append($("<td/>", { text: data[i].message }))
.append($("<td/>", {
text: data[i].created_at,
class: 'created-at',
'data-created_at': data[i].created_at
}))
));
}
}, 'json');
So, every time that I want to change the structure of the table I have to change the .php and .js files. Now, the questions is, Is there any way to store the structure of the table in one file and every times that I want to change the structure, I just change that file?
$('#contact-messages').load('contact-messages.php'). If you DO need the individual bits of data kept separate, then do send json, but include a full copy of the html and use that in a.html()call to replace the original table.