A quick example of moo.ajax
Okay, now we’ve cleared the air around AJAX, let’s look at a quick example. This is almost like the example on the mad4milk website, but just a wee different…
Right, the first thing you need to be sure of, is that you’ve got the 2 much needed Javascripts; moo.ajax.js and prototype.lite.js Funny, but without those it simply doesn’t work… :O
Link those 2 files between your head tags of your html document.
Right, next step is to write a bit of Javascript to actually use those two libraries you’ve made. I simply wrote a function that creates a new ajax instance and pass the parameters to a second php file.
<script type=”text/javascript”>
function myFunction(request){
alert (request.responseText);
}
function aStiiFunction(){
var name = $(’yourname’).value;
var mesg = $(’yourmsg’).value;
new ajax(’stii.php’, {postBody: ‘name=’ + name + ‘&msg=’ + mesg, update: $(’myelementid’), onComplete: myFunction});
}
</script>
So what happens in the script:
- You fill in your name and your message in the fields, right?
- Then you click the appropriate button.
- The button you’ve clicked calls the function named aStiiFunction()
- aStiiFunction looks at the 2 input text html elements named “yourname” and “yourmsg” and assign it to 2 Javascript variables.
- I must just quickly mention: The $(’elementName’) thingy there is a shortcut supplied to us by the lovely people developing prototype, that looks for an html element with a id name of that which you told it to. VERY nifty, hey?!
- aStiiFunction then creates a new ajax instance and you pass two paramaters to it. The first is the URI to the file/script you want to call, the second are some instructions that must go to the file/script and what to do afterwards.
So lets look at the second parameter as the first one is, uhm, obvious… The second parameter has { and } around it. That is basically only to group all the stuff in the second parameter together.
- The first item “postBody:” actually pass some parameters to your script on the “other side” (the stii.php script). To add more and more parameters, all you do is you add &varname=varvalue to the string already there.
- The second thingy there is “update:”. That gives the ajax lib the instruction “Update the HTML element with the id thats equal to ‘myElement’ with whatever it is that the script called by ajax returns…”
- The third one “onComplete:” says to ajax, “once you have received the result back, call this other Javascript function called myFunction”.
There now. THAT was not too difficult?! Ajax is simply sweet (pun intended) thanx to the okes at mad4milk!
Oh and before I rush off… Below is all the code and you can click here for a test drive, and here to download the example!
Below is the source for test.html
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en”>
<head>
<meta http-equiv=”content-type” content=”text/html; charset=iso-8859-1″ />
<meta name=”author” content=”mad4milk” />
<meta name=”robots” content=”all” />
<title>moo.ajax</title>
<script type=”text/javascript” xsrc=”prototype.lite.js” mce_src=”prototype.lite.js”></script>
<script type=”text/javascript” xsrc=”moo.ajax.js” mce_src=”moo.ajax.js”></script><script type=”text/javascript”>
function myFunction(request){
alert (request.responseText);
}
function aStiiFunction(){
var name = $(’yourname’).value;
var mesg = $(’yourmsg’).value;
new ajax(’stii.php’, {postBody: ‘name=’ + name + ‘&msg=’ + mesg, update: $(’myelementid’), onComplete: myFunction});
}
</script>
</head>
<body>
<h1>moo.ajax, 1.3kb for all your asyncronous needs.</h1><div id=”myelementid”>
this text will be (hopefully) replaced after the request is completed.
</div>
<div>
Your name: <input type=”text” id=”yourname” value=”"/><br/>
A message: <input type=”text” id=”yourmsg” value=”"/><br/>
<input type=”button” value=”Send to AJAX!” id=”sendbtn” onClick=”aStiiFunction();”/><br/>
</div></body></html>
And here is the stii.php script:
<?php
echo ‘Hello, ‘ . $_POST['name'] . ‘ here is a cool message for you! <b>’ . $_POST['msg'] . ‘</b>’;
?>
2 Comments to "A quick example of moo.ajax"
Spit it out!
ajax, javascript, moo.ajax Stii
Recent Posts
- Astalavista Wordpress!
- Lifestreaming and Twitter is making us lazy
- Days with my father
- Friday morning fail by a stripper
- Got Springleap!
- Afrigator vs Regator
- Don’t pirate music/movies! You might be forced to use Windows if you do…
- Pike > Python?
- Using Twhirl for FriendFeed
- Being anti-social SUCKS!
My Posse
- Jayx’s bloggy
- Gogo’s blog
- Go2 South Africa
- Stumble Upon
- Dave Duarte
- Wikipedia
- zlythern
- Max Kaizen
- Tresblue
- Mike Stopforth
- RafiQ
- Muti.co.za
- Employmint
- Danette’s Bloggy!
- Thinking Machine
- White African
- kiefpiet.co.za
- Skuff’s World
- Goozeberry
- Crossloop blog
- Crossloop
- Aquila Online
- Charl van Niekerk
- Derek Allard
- Code Igniter
- Carls
- Justin Hartman
- blik.co.za
- Stefano Sessa
- Uno de Waal
- Amplitude!
- bLaugh
- Tyler Reed
- Chris Rawlinson
- Stormhoek!
- 3am
- Mike Solomon
- Mobile Q and A
- Eric Edelstein
- Marc Forrest
- Imel Rautenbach
- Absolutewillie
- Vincent Maher
- Colin Daniels
- Groogle!
- Chilibean
- Paul Jacobson
- Ayelet
- Python Guru Neil
- Rails Guru Nic
- Beverley Merriman
- Miguel
- Nic Harrywhatshisname
- Chris iMod
- Geekrebel!
- Steven McD
- Belinda sweetheart!
- Henre Rossouw
- JPGeek
- Foxinni
- Adii
- Charl Norman
- Bandwidthblog
- Jason Bagley
- Simon Botes
- Auric Silverwing
- Mark Forrester
- Saul Kropman
- Fred Roed
- Sass Schultz
- Gregor Rohrig
- Catherine Lückhoff
- Toastmasters
- SAA
Filed in
- Afrigator (26)
- ajax (9)
- API (2)
- Apple stuff (10)
- Blogging (25)
- browsers (5)
- Business (28)
- Code Igniter (8)
- firefox (8)
- flock (14)
- Funnies (73)
- GeekDinner! (18)
- General and sometimes Rants (49)
- Go2SA (2)
- ideas 2.0 (14)
- javascript (12)
- Kick-ass Tools (30)
- Linux (5)
- Marketing (25)
- moo.ajax (4)
- mootools (6)
- Open Source (10)
- Programming (33)
- C# (1)
- PHP (13)
- Python (9)
- Ruby (on Rails) (9)
- RSS (5)
- Semantic Web (32)
- Social Web (57)
- Software Development (15)
- South Africa (33)
- Tagging (6)
- Techie stuff (22)
- Tshirts (3)
- Tutorials (42)
- Blogging (17)
- Flocking (6)
- muti.co.za (13)
- Web 2.0 (73)
- web development (20)
Past Stuff
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006















yeah moo.fx is awesome ^^
i got rid of script.aculo.us coz of this
but, it seems that it was a little hard to do moo.ajax on blogger
i’ve saw some ajax on wp that was some prev-next page to load your posts
but can’t do that in blogger
Hi All Experts,
I want to use AJAX (Asynchronous JAVA script with XML ). How can i Optimize the site SEO.
as Java script and flash is not recommended by search engines. Any suggestion or help is welcomed. With Regards.