What are the pros and cons of generating dynamic pages with javascript vs doing it in PHP?

As an example, I want to display certain records from some database in a table and I'm considering the following approaches:

  1. Use Javascript to get some response in JSON from some PHP page, then dynamically build the table in Javascript.
  2. Write some PHP in the page which will make the query and iterate through the result set echoing the table markup.

What are the pros and cons of each approach?

  • Too vague. There are valid uses for both approaches, if it's something as simple as you're describing go with any. If not, please share some more info. [/thought]
    – yannis
    Dec 23, 2011 at 0:09

3 Answers 3


It's generally better for your site to be able to generate all content server-side. Bots might not implement Javascript and if there's content which can only be shown by using JS, you might end up with content not indexed by search engines.

If that's a concern for your site, I would implement all content rendering server-side and make sure all content has an URL which can be reached and rendered without Javascript.

Then, if you want to use Javascript, you can reuse the server-side programming; either use Javascript to fetch the HTML (either full or a fragment) which is generated server-side and plonk it down on the page. If you are using MVC or something similar, you might be able to obtain the Model easily in JSON format and do the View stuff in Javascript too.

If your site is not concerned with crawling, do not want your content to be accessible from URLs, etc. (i.e. your site is not really a web site), feel free to do everything in JS (i.e. GMail is not a "web site", it's an application- thus it's not strange that it's mostly JS).


All things being equal, go with the simplest option.

Option 1 requires that you export your PHP data to JSON, load it through an AJAX call, then iterate and render it with JavaScript.

Option 2 only requires that you iterate over a PHP array, no JavaScript necessary.

The beauty of ajax is that you can load the data asynchronously without reloading the page, the downside is that it's more work for you and won't function properly in every browser (or for any user with JavaScript deliberately disabled).


PHP is rendered on the server.

JavaScript is rendered on the client.

PHP can take direct advantage of all of the commands, scripts, programs, databases (etc) that are available on the server.

JavaScript can not launch any commands, scripts or programs on the client's computer, nor can it access databases (not directly - it needs the data returned to it in some fashion).

I believe that JavaScript is sometimes a clumsy way to program business logic, as it's very hard to isolate your presentation logic from your business logic.

On the other hand you can easily isolate your business logic and presentation logic using PHP as a back-end language, and JavaScript as an aid to the presentation (front-end).

Why not use both?

Kill off as much of the "back-end" work as you can using PHP, then "fix up" any presentation issues using CSS + JavaScript.

Best of both worlds...

  • 2
    JavaScript can not launch any commands, scripts or programs on the client's computer, nor can it access databases Nor any other language, hopefully... (But of course Javascript does have limited access to client hardware, under certain specific conditions, as do plugin based solutions)
    – yannis
    Dec 23, 2011 at 0:21
  • 2
    i dont do business logic in javascript, merely display what i get from my business logic, so its all presentation really..
    – Daniel
    Dec 23, 2011 at 0:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.