Passing values to client side with AJAX in JSON format or returning a table with an AJAX request with PHP

I am about to write codes for presenting information, in some cases, I will get outputs as a list with a pager. I need your help about deciding between two:

  1. Passing values to client side with AJAX in JSON format and modifying HTML with DOM, maybe creating a hidden div and table somewhere and creating an instance of it, filling with values and inserting somewhere with innerHTML function. I have never tried this one, so please tell me about the drawbacks of this. This will make my View Class in javascript when leaving Model And Controller Class in PHP.
  2. Returning an HTML table with an AJAX request and inserting on page with innerHTML function (This seems to be the classical, safe solution)

  1. What are the drawbacks of handling formatting and view related
    operations in javascript?
  2. Is formatting in javascript easy?
  3. Will I be able to edit a template table for displaying values with javascript?
  4. SHould I create a div somewhere or should I build a table with string values in javascript?
  5. Do I have to use Innerhtml each time?
  6. Is it easy to build an html output using javascript code like we do in PHP? (like echo -table -tr -td -data)

for the people who automaticly ask for code, I am adding some, knowing that it is not actually necessary.

var ccomment= { 
            commentmin:10,
            commentmax:1000,
            view:function(commentid){}, //I am planning to present data with this
            viewbyopinionid:function(opinionid){},
            viewbyactorid:function(actorid){},
            insert:function(opinionid,comment){},
            remove:function(commentid){}
         }

I use PHP and JQuery; I am writing Object Oriented code in both.

Upvotes: 3

Views: 377

Answers (1)

CBusBus
CBusBus

Reputation: 2359

Php has a function json_encode which will convert an array to a json compatible string. I'd use that and set the return data type to json.

Upvotes: 2

Related Questions