Page 1 of 1

Generating HTML -- Javascript vs PHP

Posted: Fri Dec 19, 2008 11:34 pm
by Shiki
Hi,

I have this scenario (eCommerce) where I need to update a list of items in a table through AJAX every time the user changes some controls. The items can go up to around 20 in the table and each item (<td>) would contain the item's image, description, and 3 or more color thumbnails for selecting which color the user wants.

Now, I know of two ways to do the update.

1. Request generated html (<td><a><img> ... ) for the items and replace the contents of the table.
2. Request items data (id, description, image path) and generate the html for the items with javascript.

Option 1 would put more load in the server and data transfer (GET), and I think it wouldn't require too much effort for javascript to replace the items' html.

Option 2 would be lighter on data transfer but the client (browser) would require a few processing time to create the html for the items. Also, if we had a standard function (PHP) that generates html to display items which is being used for all pages, I'd be updating both the javascript html generator and the PHP function if ever we need to change the design. Option 1 would just be using the same function to generate the html.

But I'm still confused on what's the best one to use though. I'd love to hear your opinions. I tried to find some articles about this but couldn't find a good one.

Thanks a lot!

Re: Generating HTML -- Javascript vs PHP

Posted: Sat Dec 20, 2008 5:20 am
by kaszu
I would do 2nd since creating 20 tds in javascript should be fast and you can do templating in JS too (i use a little modified version, because I use smarty and wanted to use same template in JS and PHP).

Re: Generating HTML -- Javascript vs PHP

Posted: Sun Dec 21, 2008 1:23 pm
by Shiki
Thank you for the reply. That looks like a good idea.