Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-12-2011, 05:25 PM   PM User | #1
Psionicsin
Regular Coder

 
Psionicsin's Avatar
 
Join Date: Aug 2010
Location: Ann Arbor, Michigan
Posts: 334
Thanks: 51
Thanked 0 Times in 0 Posts
Psionicsin is an unknown quantity at this point
Static PHP/HTML to AJAX PHP/HTML Help

Hey guys!

I'm not well versed on AJAX, and the tutorials that I've viewed haven't answered my question in the way I needed it answered, so I figured you more seasoned people could point me in the right direction.

Right now we have a static site. It uses a mix of html & php (hopefully that doesn't matter for AJAX). The site is currently set up so that content loads in a specific div when clicking a link to another page. So because of that I SUSPECT that we shouldn't have to totally change the format of our site to implement AJAX.

With that being said could anyone either 1) point me to an AWESOME tutorial on how to AJAXify a static HTMl site or 2) attempt to inform me of the process via your own experiences and commented code?
Psionicsin is offline   Reply With Quote
Old 02-12-2011, 05:29 PM   PM User | #2
Psionicsin
Regular Coder

 
Psionicsin's Avatar
 
Join Date: Aug 2010
Location: Ann Arbor, Michigan
Posts: 334
Thanks: 51
Thanked 0 Times in 0 Posts
Psionicsin is an unknown quantity at this point
Also...is it possible to use nested AJAX requests?

Example:

Code:
<div id="ajax1">
    <div id="ajax2">
    </div>
</div>
...and so on?
Psionicsin is offline   Reply With Quote
Old 02-12-2011, 06:44 PM   PM User | #3
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by Psionicsin View Post
Also...is it possible to use nested AJAX requests?

Example:

Code:
<div id="ajax1">
    <div id="ajax2">
    </div>
</div>
...and so on?
try this one:

http://www.xul.fr/

best regards
oesxyl is offline   Reply With Quote
Old 02-12-2011, 06:54 PM   PM User | #4
Psionicsin
Regular Coder

 
Psionicsin's Avatar
 
Join Date: Aug 2010
Location: Ann Arbor, Michigan
Posts: 334
Thanks: 51
Thanked 0 Times in 0 Posts
Psionicsin is an unknown quantity at this point
Quote:
Originally Posted by oesxyl View Post
try this one:

http://www.xul.fr/

best regards
Thanks. Will look at this thoroughly.
Psionicsin is offline   Reply With Quote
Old 02-12-2011, 11:54 PM   PM User | #5
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
Based on your description, I'm going to assume that there's a single PHP file that, depending on the query string, builds different pages that differ from each other only in the contents of that one div.

What you can do is check in that PHP script whether it has been requested normally or via AJAX, for example like this:

PHP Code:
function isAjax() {
    return (isset(
$_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'));

In case it's a normal request, you just let it build the page like usual; in case of an AJAX request you just let it echo the contents of that div.

That way, your AJAX function can just request the usual link, and put the response directly into the div, without having to parse it and find the relevant content first.

As for the nested AJAX requests — I don't really know what you mean. There's no such thing as nested AJAX requests. AJAX requests are just normal HTTP requests, with the difference that the response isn't loaded in a new page, but sent to your Javascript function.

What you do with that response (e.g. put it into a nested div) is completely up to you and has nothing to do with AJAX.
venegal is offline   Reply With Quote
Old 02-14-2011, 09:54 PM   PM User | #6
Psionicsin
Regular Coder

 
Psionicsin's Avatar
 
Join Date: Aug 2010
Location: Ann Arbor, Michigan
Posts: 334
Thanks: 51
Thanked 0 Times in 0 Posts
Psionicsin is an unknown quantity at this point
Quote:
Originally Posted by venegal View Post
Based on your description, I'm going to assume that there's a single PHP file that, depending on the query string, builds different pages that differ from each other only in the contents of that one div.
Nope. I think I may have confused you a bit. There are as many .php pages as there are content pages. I was just stating that although every "containing" .php page is the same, the only thing that changes from page to page is the content of a specific div. Which I, personally, find to be highly inefficient as compared to just having one .php page and AJAXing the content into the only changing div.

So, currently, there are NO requests going on at all as the whole site is strictly static. That clear it up?
Psionicsin is offline   Reply With Quote
Old 02-15-2011, 02:43 AM   PM User | #7
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
Well that just means that you're doing it wrong, because you're duplicating information in all those files. You might want to switch to a single processing file solution, in order to get rid of all that code duplication, and maybe look into URL rewriting, so you can keep your established URLs.

That step doesn't have anything to do with AJAX, it's just generally good practice. Once you've done it, though, it should be easy to switch to AJAX, like already explained (if you still feel you need it).
venegal is offline   Reply With Quote
Old 02-16-2011, 05:37 PM   PM User | #8
Psionicsin
Regular Coder

 
Psionicsin's Avatar
 
Join Date: Aug 2010
Location: Ann Arbor, Michigan
Posts: 334
Thanks: 51
Thanked 0 Times in 0 Posts
Psionicsin is an unknown quantity at this point
Quote:
Originally Posted by venegal View Post
Well that just means that you're doing it wrong, because you're duplicating information in all those files. You might want to switch to a single processing file solution, in order to get rid of all that code duplication, and maybe look into URL rewriting, so you can keep your established URLs.

That step doesn't have anything to do with AJAX, it's just generally good practice. Once you've done it, though, it should be easy to switch to AJAX, like already explained (if you still feel you need it).
Ok, well this is exactly what we're looking for (http://css-tricks.com/video-screenca...namic-content/). Is this a viable option without going through all of that?

We're just going to be using it for the very basic of basic functionality which is just refreshing content in a div without reloading the whole page. Nothing more advanced over that. And what you mentioned seemed a lot harder than how he's making it sound on the video.

But if he's doing the same thing you're speaking of, then kudos.
Psionicsin is offline   Reply With Quote
Old 02-16-2011, 06:14 PM   PM User | #9
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
I see. The guy in the video is not doing what I suggested. What he's doing is fetch the whole new page via AJAX, extract the contents of that one div, and put them in the page.

My suggestion is just generally good advice, because code duplication is a really bad thing and can cause high maintenance costs, so if you were building the site from scratch, you should definitely do it that way. But if on your existing site you were ok with this up until now, you may be ok with it in the future, too.

You can certainly do it like the guy in the video, but you have to be aware of the following:
You mentioned earlier that you find loading a new page when only one div changes "to be highly inefficient". In fact, what you are trying to do (although it might look better and more efficient to the user) is in fact less efficient than a normal page refresh. Since you are not getting rid of duplicated HTML in order to be able to serve only the relevant content, the AJAX response will in fact contain the whole page (so, just the same amount of data there would be with a normal page refresh), which then has to be parsed (which also has to happen with a page refresh), and the relevant content has to be found, extracted, and added to the DOM (with doesn't happen with a page refresh).

Arguably, the render time itself might be a bit longer when refreshing the page, but still, don't fool yourself into thinking you're doing it this way because of efficiency considerations, because you're not. It's about user experience only. That's absolutely fine, though, as long as you don't break browser history or deep link bookmarkability/sharability in the process.
venegal is offline   Reply With Quote
Old 02-16-2011, 10:07 PM   PM User | #10
Psionicsin
Regular Coder

 
Psionicsin's Avatar
 
Join Date: Aug 2010
Location: Ann Arbor, Michigan
Posts: 334
Thanks: 51
Thanked 0 Times in 0 Posts
Psionicsin is an unknown quantity at this point
Quote:
Originally Posted by venegal View Post
I see. The guy in the video is not doing what I suggested. What he's doing is fetch the whole new page via AJAX, extract the contents of that one div, and put them in the page.

My suggestion is just generally good advice, because code duplication is a really bad thing and can cause high maintenance costs, so if you were building the site from scratch, you should definitely do it that way. But if on your existing site you were ok with this up until now, you may be ok with it in the future, too.

You can certainly do it like the guy in the video, but you have to be aware of the following:
You mentioned earlier that you find loading a new page when only one div changes "to be highly inefficient". In fact, what you are trying to do (although it might look better and more efficient to the user) is in fact less efficient than a normal page refresh. Since you are not getting rid of duplicated HTML in order to be able to serve only the relevant content, the AJAX response will in fact contain the whole page (so, just the same amount of data there would be with a normal page refresh), which then has to be parsed (which also has to happen with a page refresh), and the relevant content has to be found, extracted, and added to the DOM (with doesn't happen with a page refresh).

Arguably, the render time itself might be a bit longer when refreshing the page, but still, don't fool yourself into thinking you're doing it this way because of efficiency considerations, because you're not. It's about user experience only. That's absolutely fine, though, as long as you don't break browser history or deep link bookmarkability/sharability in the process.
I think I'm starting to understand where you're coming from. Is there an article or anything you can link me to for thorough explanation of your proposed method?
Psionicsin is offline   Reply With Quote
Old 02-16-2011, 10:19 PM   PM User | #11
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
I don't really have an article or tutorial handy, but basically what I was talking about before is using some sort of template, which means, a single file that is able to generate many different pages (usually based on URL parameters). That doesn't mean you have to use some clunky template engine — PHP itself is perfectly able to do that. You'll probably find some useful explanations if you search around a bit.

That said, when your site is already set up the way it is, it isn't too bad to do the AJAX thing the way you proposed (fetching the whole site and extracting the relevant information). I just said that efficiency considerations shouldn't be the reason to do it that way, because that wouldn't make any sense. Doing it for a better user experience is perfectly ok, though.
venegal is offline   Reply With Quote
Old 03-14-2011, 11:04 PM   PM User | #12
Psionicsin
Regular Coder

 
Psionicsin's Avatar
 
Join Date: Aug 2010
Location: Ann Arbor, Michigan
Posts: 334
Thanks: 51
Thanked 0 Times in 0 Posts
Psionicsin is an unknown quantity at this point
Quote:
Originally Posted by venegal View Post
I don't really have an article or tutorial handy, but basically what I was talking about before is using some sort of template, which means, a single file that is able to generate many different pages (usually based on URL parameters). That doesn't mean you have to use some clunky template engine — PHP itself is perfectly able to do that. You'll probably find some useful explanations if you search around a bit.

That said, when your site is already set up the way it is, it isn't too bad to do the AJAX thing the way you proposed (fetching the whole site and extracting the relevant information). I just said that efficiency considerations shouldn't be the reason to do it that way, because that wouldn't make any sense. Doing it for a better user experience is perfectly ok, though.
Ok...here's a skeleton/template for the new site that I've made so far.

Would you say that this is a good setup to get started with for the procedure you've suggested?
Psionicsin is offline   Reply With Quote
Old 03-15-2011, 03:47 PM   PM User | #13
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
I can't see your server side code, only the generated HTML, so I couldn't really say. If you can make it work for the whole site without duplicating code, I'd say it's probably a good setup.
venegal is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:19 PM.


Advertisement
Log in to turn off these ads.