Please see following examples first, and which one is better?
Could you compare some aspect such as performance, programming, design, load, user experience, maintenance, security or any other aspect which I have not expected.
Can these aspect help me to decide which one is better?
<script>
$(document).ready
(
function()
{
var message={..something..};
$.ajax
(
{
url:'get_content.php',
data:message,
type:'post',
cache:false,
success:function(data)
{
foreach(data.array)
{
$('#content').append($('<tr><td>'+data.array[key]+'</td></tr>'));
}
},
error:function(){alert('error');}
}
);
}
);
</script>
<table id="content">
</table>
<table id="content">
<?php
query sql;
while(row=fetch result)
{
echo '<tr><td>'+row[field]+'</td></tr>';
}
?>
</table>
You’re comparing apples to oranges.
AJAX is about your only (reasonable) means to grab data from the server on behalf of the client without a full page refresh. However, you can use an assortment of different languages server-side to render the data necessary for AJAX to proceed. (ASP, PHP, …).
It’s up to you and what the rest of your site is developed in really. If you want an absolute (no failure) method of producing content, a PHP dump is about the best way. It’s guaranteed to be visible and won’t break depending on client support (maybe they have NoScript plugin?).
However, if there is a lot of data, it’s sometimes better to spread the load over multiple calls so that the client has at least a semi-complete portion of the page visible, and the data comes later. (This is generally how Facebook proceeds–they give you the general layout in the first fraction of a second, then the rest of the content comes once the framing is complete).
One thing to note though is that it’s not an either/or kind of decision. You can use AJAX with a PHP fall-back, you just have to have code in place to test the client and relay that information back to the server for an informed decision.