PHP: ajax without XMLHTTP-request
Got a request to make a quick hack; but this quick hack will be immensely helpful and able to cut like 3 hours of work / employee everyday. Naturally, seeing such huge cost / benefit ratio, I agreed to make it my priority.
The hack is to make pricing become automatic for a client. At the moment, they have to look up a pricing table manually for each order that they received. Even though they’re pretty big, built their own IT system from scratch, they’re still doing this particular topic manually. I can’t believe that their previous developer missed this stuff.
Until I realized the full scope of the job…. suffice to say, it’s complicated enough to scare most programmers. Their pricing scheme almost got no pattern at all, almost fully customized for every client. Yet we can’t afford to just enter the whole thing as it is to the database - it’d be too cumbersome.
The previous developers spent 3 years developing the system, and still managed to avoid this particular bit.
More time would be required to develop the algorithm and proper database schema, than the actual coding itself. Anyway, more to that probably later; one particular thing that I’d like to avoid in this job is the XMLHTTP-request stuff.
Why ? Because I’m too lazy
too lazy to implement it myself from scratch, and I don’t have the time to use the available implementations (and deal with their quirks/bugs).
I like things as simple and as fast as possible. I think I’m kinda a control freak, yes that explains it.
Anyway, here’s the requirement:
1. Once the staff entered an order into the system, he’ll clink into a link on the form.
This link will fill the textbox next to it with the right price for that order
That’s it. What’s going on behind the curtain is of course much more than that. Here you go :
1. The link actually invokes a JavaScript function called do_ajax()
2. The function will then append a JS element - which actually a PHP script. Thus executing the “AJAX” call
3. The JavaScript function can communicate with the server-side PHP script by way of GET method.
4. The PHP script produced its output as JavaScript code.
As simple as that.
Here are the details:
The HTML form
cn-ajax.js
cn-ajax.php
It may look complicated at first, but if you think of the cn-ajax.js as a blackbox,
then it became very simple.
NOTE: Another plus is that this form become usable for mobile warriors as well. Sure today’s PDA can do JavaScript - but speedily? Thought so.
This trick moves most of the processing to the server, with as little JavaScript code as possible. Therefore giving the best performance for these audience.
Other advantages are : clean, cross browsers, adherence to standards.
(thanks rendy ak)
Credit goes to PHPit.com for figuring these out.
TIP: Use Firefox while debugging this stuff. Its JavaScript console was a massive help to me.
OK, back to work.

August 8th, 2006 12:11
See also the FireBug extension for debugging in Firefox.
August 8th, 2006 13:35
nambahin ?? IFRAME jg bisa dipake..
lalu.. cobain prototype deh
August 8th, 2006 13:37
walah.. ada yg ilang. mestinya
nambahin tag script ?? IFRAME jg bisa dipake..
August 8th, 2006 14:44
Kreatif banget!
Otomatis cross browser, standards compliant and clean. Tapi nanti kalau sering2 dipanggil di_ajax-nya bakal ada banyak ‘tempelan’ element script dong di DOM-nya?
August 8th, 2006 14:45
Ups typo, mxd-nya do_ajax()
August 8th, 2006 15:08
Hmm.. yet another AJAX-wrapper-library. I easily get lost with enormous amount of such wrapper nowadays (check ajaxpatterns.org and ajaxian.com).
Sooner or later, we will get our hand dirty with XHR. Speaking from my painful experience
And when the time comes, I agree with Ronny, Firebug is our best friend.
August 10th, 2006 00:40
iang - thanks untuk rekomendasinya. btw; <script> dan IFRAME ? mungkin bisa dibagi juga tipsnya
ak - sepertinya sih begitu, tapi kalau dicek di “view source”, tidak kelihatan (karena browsernya fetch yang aslinya, bukan yang sudah termodifikasi). anyway, yang penting bisa ajax, hoho
andry - yes, I think XHR is inevitable. But if I can avoid using it as long as possible (with help from this little gem), then when I finally need it, the selections are already in a much mature state. I’d rather let others to enjoy the bugs if I can help it
August 11th, 2006 01:15
Mungkin akan kelihatan dengan Firefox DOM inspector, atau plugin view generated source. Tag-tag tidak penting seperti itu nantinya bisa memenuhi memory (meski mungkin tidak terasa langsung).
Mungkin bisa ditambahin attribute id, dan kode JS yang ‘baru dateng’ itu bisa otomatis mencari remove elementnya sendiri?
August 30th, 2008 03:49
I’ve used this method before. It’s actually a lot more compatible than I thought it would be. It’s a little slow however, and doesn’t give you any browser history, which is just like the XMLHTTPRequest method. If you want do have history, you need to use the IFRAME method. Not all application would benefit from having history though.