Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Why to generate the HTML in PHP:</p> <ul> <li>JavaScript should define the behaviour, not the content.</li> <li>Creating in JavaScript requires more markup (multi-line strings aren't as easy as in PHP).</li> <li>It's harder to maintain if your HTML is generated in several places (PHP &amp; JS).</li> <li>You could use jQuery's DOM manipulation functions to create your HTML, but you're shooting yourself in the leg on the long run.</li> <li>It's faster to create the HTML at the server than on the browser (in computational sense).</li> <li>Looping is easier with PHP &ndash; it's easy to generate table markup.</li> <li>You retain some kind of combatibility if the user has JavaScript disabled if you output the markup on page load.</li> </ul> <p>Why to generate the HTML in jQuery:</p> <ul> <li>You'd save some bandwidth.</li> <li>Binding events might be simpler.</li> </ul> <p>So, I'm in favour of the first option, generating the HTML in PHP.</p> <hr> <p>Visual comparison of the two methods, creating a simple table. &nbsp;</p> <h3>Option 1, using PHP:</h3> <pre><code>// PHP $html = '&lt;table&gt;'; foreach($data as $row) { $html .= '&lt;tr&gt;'; $html .= '&lt;td&gt;&lt;a href="#" class="button"&gt;Click!&lt;/a&gt;&lt;/td&gt;'; $html .= '&lt;td&gt;'.$row['id'].'&lt;/td&gt;'; $html .= '&lt;td&gt;'.$row['name'].'&lt;/td&gt;'; $html .= '&lt;/tr&gt;'; } $html .= '&lt;/table&gt;'; echo $html; ?&gt; // jQuery $('#container').load('handler.php', function() { $('#container a.button').click(function() { // Do something }); }); </code></pre> <p>&nbsp;</p> <h3>Option 2, using jQuery:</h3> <pre><code>// PHP echo json_encode($data); // jQuery $.ajax({ url: 'handler.php', dataType: 'json', success: function(data) { var table = $('&lt;table /&gt;'); var len = data.length; for(var i = 0; i &lt; len; i++) { var row = $('&lt;tr /&gt;'); var a = $('&lt;a /&gt;').attr('href', '#').addClass('button'); row.append($('&lt;td /&gt;').append(a)); row.append($('&lt;td /&gt;').html(data[i].id); row.append($('&lt;td /&gt;').html(data[i].name); table.append(row); } table.find('.button').click(function() { // Do something }); $('#container').html(table); } }); </code></pre> <p>From a design / ease of coding / beauty point of view, I'd definitely say go with the PHP approach.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload