php - Building web pages on the server-side or client-side? -
i've wondered how decide on choosing between using server-side code versus client-side code build html pages. i'll use simple php vs javascript/jquery example further explain question. advice , comment appreciated.
say i'm present web page user select type of report in web page. makes more sense?
for server-side creation, i'd this:
<div id="reportchoices"> <?php // filename: reportscreen.php // sake of simplicity, database returns following rows // indicates type of reports available: $results = array( array("htmlid"=>"battingaverage", "htmllabel"=>"batting avg report"), array("htmlid"=>"homeruntotals", "htmllabel"=>"home run totals report"), ); foreach ($results $data) echo "<input type='radio' name='reporttype' value='{$data['htmlid']}'/>{$data['htmllabel']}"; ?> </div>
using client-side code, i'd javascript build page following:
<!-- filename: reportscreen.html --> <div id="reportchoices"> </div> <!-- put in document.ready handler, of course --> <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 need on server data dump php script such as:
<?php // filename: rt.php // again, let's assume returned db regarding available report types $results = array( array("htmlid"=>"battingaverage", "htmllabel"=>"batting avg report"), array("htmlid"=>"homeruntotals", "htmllabel"=>"home run totals report"), ); echo json_encode($results); ?>
this simple example, this, see pros , cons in different area.
1 - server-side solution has advantage of being able hide of actual programming logic behind how built. when user looks @ page source, see already-built web page. in other words, client-side solution gives away source code , programming logic on how things built. use minifier make source more cryptic.
2 - client-side solution transfers "resource load" onto client system (i.e. browser needs use client's computer resources build of page) whereas server side solution bogs down, well, server.
3 - client-side solution more elegant when comes maintainability , readability. again, have used php libraries modularize html controls , make lot more readable.
any comments? in advance.
con (client solution): client-side solution relies on client execute code properly. have no control on client system execute code, it's harder ensure consistently give same results server-side solution.
this particular problem doesn't seem need client-side solution, it? i'd stick server-side solution. work there foreach
loop 1 echo
, that's not resource heavy (unless you've profiled , know is)? , resulting code in 1 place , simpler.
Comments
Post a Comment