views:

66

answers:

3

I am currently working on a project that lets users post comments with jquery and ajax. So far it is using Json and retunring several items, username, comment text, user photo url, comment ID number and stuff like that, I then need to use some sort of template to make all this data go into the correct div's before adding it all to the screen.

I am new to using javascript so this is a hard task for me. I am now considering the easy route.

Just have my PHP backend script return the whole block of code, div's and everything in place but I am wondering is this a bad idea? More importantly is it a bad idea with json?

Here is an example of a block of code that needs to be added to the screen when a comment is posted

<li class="admin" id="comment-1371"> 
 <div class="photocolumn"> 
  <!-- START Photo block -->
  <div class="imageSub" style="width: 100px;"> 
   <img class="male" src="http://cache2.mycrib.net/images/image_group34/0/39/T_653807517aff2b1f5662d865b40d87d527c8eb.jpg" alt="Something" width="100"/>
   <div class="blackbg"></div>
   <div class="label">JasonDavis</div>
  </div>
  <!-- END Photo block --> 
 </div><!-- END photocolumn -->

<div class="commenttext"> 
  <p>02/12/3009</p>
  <p>sample text for comment area!</p>
</div>
<!-- END COMMENTTEXT --> 
    </li>
A: 

I don't see a problem with returning HTML via AJAX. A bonus of this is that you can generate most of the HTML in a view in PHP and still keep things fairly clean.

Tokenizing your data into an object is nice for re-use but can be overkill for a one-off.

Mark L
A: 

Go the easy route, I can see no reasons of going with JSON array.

ilya.devyatovsky
+1  A: 

I would say it depends on the situation/application. For instance I would use json and templating for a flight/hotel etc result screen. Why return 50k's worth of the same markup when a 4k json object will do and will allow for rapid clientside sort/filter. If you dont need quick clientside filtering/sorting then responding with dom fragments is ok. Horses for courses.

redsquare