I always wondered how to choose between using server-side code and client-side code to create HTML pages. I will use a very simple php vs javascript / jquery example to further explain my question. Your advice and comments are greatly appreciated.
Say I'm going to present a webpage to a user to select the type of report on my webpage. What makes sense?
For server-side creation, I would do the following:
<div id="reportChoices">
<?php
$results = array(
array("htmlID"=>"battingaverage", "htmlLabel"=>"Batting AVG report"),
array("htmlID"=>"homeruntotals", "htmlLabel"=>"Home Run Totals report"),
);
foreach ($results AS $data)
echo "<input type='radio' name='reportType' value='{$data['htmlID']}'/>{$data['htmlLabel']}";
?>
</div>
Using client-side code, I would get javascript to create the page, as shown below:
<div id="reportChoices">
</div>
<script type="text/javascript">
$.getJSON("rt.php", {}, function(data) {
var mainDiv = $("#reportChoices");
$.each(data, function(idx, jsonData) {
var newInput = $(document.createElement('input'));
newInput
.attr("type", "radio")
.attr("name", "reportType")
.attr("value", jsonData["htmlID"])
mainDiv.append(newInput).append(jsonData["htmlLabel"]);
});
};
</script>
All I need on the server is a php script data dump, for example:
<?php
$results = array(
array("htmlID"=>"battingaverage", "htmlLabel"=>"Batting AVG report"),
array("htmlID"=>"homeruntotals", "htmlLabel"=>"Home Run Totals report"),
);
echo json_encode($results);
?>
This is a very simple example, but from this I see the pros and cons in different areas.
1 - , , . , , , -. , , . minifier, .
2 - " " (.. ), , , .
3 - , , , . , php-, HTML .
? .