Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Since I discovered the joys of AJAX, I tend to do all my requests to the server using AJAX. Is this a good idea?

According to you, what should I do in PHP and what should I do in AJAX?

I like to do my requests to the server with AJAX because I can use it as a kind of web service.

share|improve this question

closed as too broad by gnat, MichaelT, GlenH7, Kilian Foth, jwenting Sep 22 '14 at 9:18

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

AJAX is a technique, not a language. You can write the server-side component of an AJAX call in PHP if you want to. As far as PHP cares, it's just a different type of output.

As to the other question, of "what should I do with AJAX and what should be a full page reload", anything that you would let someone type in a URL for, or come directly to your site from another page for, can be a page reload. Although if you have a robust enough AJAX implementation, you can go ahead and have all of your site's internal navigation be AJAX calls.

(And "come directly to your site from another page" also means right-clicking to open in a new tab.)

share|improve this answer
I don't know if I have a robust AJAX implementation because I never found an example. – hlapointe May 18 '13 at 16:49

AJAX is used in cases where you can speed-up user actions by avoiding useless page refresh. In general:

  • Use AJAX when an action of the user affects a small part of the page and the action is done regularly.

  • Don't use it when an action of the user changes nearly the whole page or the action is done rarely.

Since you're complaining in your comment of the answer by DougM to be unable to find an example, here's an example which considers different scenarios of an application, indicating for each whether AJAX can be used and what criteria should be taken in account to make the choice.


Let's take a web page where users can view and add post-its (which would be useful in a context when they need to take short notes, like a phone number, in order to access them later from everywhere). For the sake of simplicity, we will not discuss registration/logon.

First, the user arrives. This is an ordinary request, since there is no page which can be used as a base for AJAX. So no AJAX here.

Then, the user fills an empty post-it and submits the new content. Here, AJAX can be useful: instead of forcing the user into the page reload, sending again all the menus and logo at the top and copyright notice and links at the bottom, etc. A more elegant way would be simply to shake the post-it to indicate that it's posted, or change its appearance in some way, and voila.

If the post-it is removed, AJAX can be useful again. Instead of refreshing the whole page, just remove the concerned post-it from the list. The situation is similar when the user removes the post-it, then clicks on "Undo removal" link which appears for a few seconds.

Now, the user wants to filter post-its (we assume every post-it is displayed on the page) in order to search for one. Here, you don't do requests at all (unless you want to keep a log of what was searched). You simply hide, through JavaScript, post-its which don't match, and keep showing the ones which are matching the filter.

Let's assume now that not every post-it is displayed on the page, i.e. there is a "Page 1 · 2 · 3 · … · 16" bar. When the user switches from page to page, you might use AJAX, or you may avoid using it. In fact, loading all the post-its on a page may take some time: nearly the same time as loading the whole page with top logo and menus and bottom copyright notice.

Finally, what about a user who removed a post-it few days ago, and now wants to recover it? No matter how much content is changed, you don't use AJAX here. In fact, the feature will be accessed by only a few users, and not very often. Implementing AJAX and adding complexity for a feature which is barely used isn't worth it.

share|improve this answer

These are two different type! PHP is a server side and you can do anything with it. continue to show on client side anything you want with ajax and post your data to PHP. like

share|improve this answer

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