11

Why use AJAX for dynamic web pages when you can do it only with php?

4
  • 1
    For instance, the index.php page of the site, can just switch the content of the body via only php? So why even bother with AJAX? Commented Apr 2, 2011 at 20:43
  • 2
    It can't, though. PHP can certainly deliver a different page on refresh, but the browser will have to load the whole page again. And how do you propose to do something like Facebook Chat without AJAX? Commented Apr 2, 2011 at 20:47
  • You can't do it only with PHP, that's why AJAX exists. Commented Apr 2, 2011 at 20:48
  • 5
    The term dynamic is used in two different contexts here. PHP is creating the content dynamically whereas one can say that Ajax is loading the content dynamically. E.g. you can use Ajax to load content created by PHP. They work together, they are not exclusive. Commented Apr 2, 2011 at 20:53

3 Answers 3

18

The main reason to bother with AJAX is User Experience (UX).

Now AJAX won't necessarily improve UX in every single instance so in a lot of places sticking with pure PHP is perfectly okay.

But imagine the case where you have a text field on the site and a link to vote on something. Kinda like this site. When you add AJAX your users won't loose the text they entered in the textfield when they decide to vote on the link! How incredibly useful!

So if you care about your user's experience it is a good idea to use AJAX in situations like that.

Sign up to request clarification or add additional context in comments.

6 Comments

You are wrong... when you eg vote on SO, you only send small request, which is better for performance then generating whole page again (header, posts, votes, related questions)...
@webarto that is also true - but your server's performance problems can be solved with buying more servers unlike your UX failures.
@webarto I don't really see how you saying 'You are wrong...' makes sense. You are basically restating what Jakub Hampl stated in his answer, which is that Ajax is a great idea for situations like submitting a vote without reloading the whole page.
@sergeidave I might have been drunk... he didn't mention performance but "your users won't loose the text they entered"... I'm sure XHR wasn't created because that was "incredibly useful".
@webarto Agreed, he did not mention performance at all and instead was happy to only point out an example of how AJAX can be 'incredibly useful' in regards to UX. Do you not agree that UX is a big reason why XHR is used by many developers? It may very well be the case that some care more about UX and others about performance, but I think at that point the discussion becomes more a matter of what are your particular needs at the moment.
|
5

PHP creates and outputs the Content to the Client Browser as it's a Server-Side Language and that's what it was built for, so on a request your code will access database, files etc. and then output the constructed html/text to the client.

Ajax just gives the User a more Desktop like feel. For example deleting a record and instead of the entire page reloading just letting the one element disappear from say a list and letting the server know that the record is to be deleted. But Remember to let the User know when you are busy sending data to the server (With a progress bar in .gif format for example). As lot's of user feel that if nothing happens on the screen to notify them, that the application is frozen which means they will either reload the page or just try to click the button again.

But you will need to provide some sort of compatibility with browsers that have Javascript disable and thus cannot use your AJAX functions, just something to keep in mind.

2 Comments

Well for instance, MAIN reason or core purpose of AJAX is to avoid loading content of the ENTIRE WEB PAGE/SITE when NOT NEEDED.
But isn't that what "deleting a record and instead of the entire page reloading just letting the one element disappear" says? Or is my English that bad :D
2

AJAX stands for Asynchronus Javascript and XML, meaning that a page can get new data, without having to reload a page.

PHP cannot send data without reloading the whole page. A user has to press a button, to send data.

An example of AJAX is for example google suggestions or the tag suggestions on this website.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.