+2  A: 

You can do this many ways:

  1. You can create a bunch of elements in jQuery via $('<div/>').html('Hi Mom') and build up your DOM that way
  2. You can build up the HTML you want as part of your ajax return set (wastes bandwidth)
  3. You can create a template and change values.

IE:

<div id="someTemplate" style="display: hidden">
    <div class="username"></div>
    ...
</div>

Now you can get the contents of someTemplate, duplicate it, fill in the details by classname, and you're good.

Eric Anderson
+1 I would look at jTemplates for this
redsquare
@redsquare, I have had to do something like this myself but have never looked at jTemplates (I haven't looked for anything, actually). Thanks for mentioning it!
strager
+1  A: 

If you are generating a lot of HTML data then you could have your PHP script return the HTML and then append it using:

$.post('/Url/', data, function(html) {
  $('div#loader').append(html);
}, "html");

The advantage to this is that it's easy and allows you to reuse the HTML generation on the server. The disadvantage is it uses more bandwidth than a Json implementation.

The alternative would be to have your script return Json and build up your HTML on the client via DOM manipulation.

$.post('/Url/', data, function(x) {
  var elm = document.createElement('div');
  elm.appendChild(document.createTextNode(x.comment);
  $('div#loader').append(elm);
}, "json");

This uses less bandwidth than returning HTML but will be more complicated to create/maintain if the HTML changes.

David G
"disadvantage is it uses more bandwidth"- I think that it's safe to assume that the HTML returned from a comment generation will not be the one that consume most of the bandwidth.. so.. this is another form of premature optimization :)
amikazmi
@amikazmi True, but it's still a disadvantage when comparing against a Json implementation. I can't speak for the amount of HTML jasondavis will be returning. I've updated my answer to make it a bit clearer.
David G
+1  A: 

I would absoluteley not recommend that you send HTML-markup in your requests. If you use JSON, for example, you will save bandwidth and your users will be happier with a site that loads up faster.

It's better if you use jQuery and JavaScript to clone a template from your page (can be a hidden div for example):

<div class="comment-template">
   <div class="header">{title}</div>
</div>

And then in your JS:

$.post('action', data, function(data) {
      var template = $('.comment-template');
      var placeholder = $('#somePlaceholder');

      for(int i = 0; i < data.length; i++) {
         var comment = template.clone();
         comment.html(comment.html.replace('{title}', data[i].title));
         comment.appendTo(placeholder);
      }
   },
   "json"
);

This sure will work alot faster than including the markup in your requests.

Mickel
This is what I was hoping, to use some sort of template. My script is using a json response right now the json returns, comment text, date the users ID number and photo URL are in a PHP session variable so i'm guessing I need to add them to a JS var? Also your line above that has the replace title part, would I need to do a line like this for each item, name, photo, comment?
jasondavis
For more advanced templates you could use helper libraries as someone just suggested (http://jtemplates.tpython.com/). Else you would have to replace each value or use another jQuery selector and a placeholder in your template.The parameter "data" that are used in the callback-function when calling $.post is what will contain your JSON object (that you return from your php-page). With this you can do whatever you want.
Mickel