views:

302

answers:

5

I just started making a PHP/AJAX site and I was wondering where I should add the functionality to "only include the content".

Should I have if statements in my header and footer scripts that check for a "ContentOnly" get parameter? And then append that parameter to the query strings in my AJAX calls?

Or should I use a filter on the AJAX side to only grab the main content Div. Like in jquery:

$(this).load("/myPhpPage.php #mainContentDiv")

The first method would be faster right? But the second method would keep the AJAX functionality on the presentation side, and out og my PHP.

Or is there a third method? I want to keep my PHP scripts intact, so that if oyu go to them directly, the header and footer are there. Thanks in advance!

+6  A: 

Please don't use AJAX for the sake of using AJAX.

That said, most AJAX libraries pass a X-Requested-With HTTP header indicating that it's an AJAX request. You can use that header to decide whether to include your header/footer etc.

ceejayoz
Amen to "Please don't use AJAX for the sake of using AJAX"
Noah Goodrich
X-Requested-With HTTP Header is a totally unneccessary solution to a problem that doesn't exist. If you want HTML say you want HTML if you want XML or JSON (anything else) data say you want XML or JSON data in the request. I don't get why this is upvoted so much. Maybe someone can enlighten me ?
Martijn Laarman
I don't understand what all the confusion is about this post... I want to have my site AJAX enabled, but also have the pages load if JavaScript is NOT enabled. In which case, I will use the X-Requested-With HTTP Header. However, when SHOULD you use AJAX?
SkippyFire
Martijn, the thing is, for a non-AJAX page load you'd want everything - header, footer, navigation, etc. For an AJAX page load you might just want the main content div, in which case X-Requested-With is very helpful.
ceejayoz
+1  A: 

Depending on what paradigm you're using to handle page requests, I would generally recommend that you place such handling in the controller script. Handling view rendering logic that is specific to the type of request being made is entirely appropriate to place in the controller layer.

As ceejayoz suggested, you can use the X-Requested-With header (make certain that it is getting passed by your javascript library's ajax functions!) to verify in the controller script what the source of the request is and then render the view appropriately.

Noah Goodrich
+1  A: 

Adding to whats been said, your application shouldn't be aware of the rendered view. If you are making a call from javascript - javascript should know the context in which the call was made.

What this means is that the return handlers should know what they are handling during callback. The best method I've found for these types of transactions is to package objects in JSON that describe whats being called and who called it. Then when things are returned you can append some of these properties to the returned object. For instance if the same callback handler is used for everything you can simply pass the reference of the context back in this returned object.

Again though, don't use AJAX unless it serves a real purpose.

Syntax
A: 

X-Requested-With HTTP to indicate its a Ajax request is rather redundant. Ajax calls should never request HTML like the browser does!

In most cases AJAX calls shouldnt return html nor plain text. Make sure you specify the correct mime-type when doing the request to a page and have the page switch on the mime type used. This way when the browser request HTML it gets HTML when the AJAX call requests XML or JSON or if you must Plain text it will get the data in that format.

Edit: AHAH is a better term in this case indeed (technicality tho). I percieved content as data in the OP, which is why i was pushing mime-type so much.

I do however still think that getting ALL the content through ajax is a bad design, you lose more then you gain. It's better to go to a new page and expose some AJAX functionality within the content, IF needed.

And if that functionality AHAH's a small errorbox that doesn't need to be pushed in the history (which content does) for example so be it even my orthodox mind can live that :)

Even if you have a header which requires heavy calculation (i.e a treeview) there are other cache options that would benefit both the javascript-abled as -disabled as suppose only javascript-abled.

Thanks for the comments btw, i really did not want to come off as concieded but can see the note i struck was just that.

Martijn Laarman
Retrieving HTML works just fine. Mere orthodoxy isn't a good enough reason to avoid a useful technique. If it's the term AJAX that's bothering you, just call it AHAH instead (http://en.wikipedia.org/wiki/AHAH).
ceejayoz
Using Ajax methodologies to return HTML works splendidly. It seems like an awfully conceited remark to say that Ajax CANNOT return HTML. Though if it really bothers you we can call it 'AHAH' instead.
Noah Goodrich
AHAH is a better term in this case indeed (technicality tho). I percieved content as data in the OP. I still think that getting ALL the content through ajax is a bad design, you lose more then you gain. It's better to go to a new page and expose some AJAX functionality within the content,IF needed.
Martijn Laarman
And if that functionality AHAH's a small errorbox that doesn't need to be pushed in the history (which content does) for example so be it even my orthodox mind can live that :)
Martijn Laarman
A: 
grantwparks