Ajax, not the cleaner, the asynchronous JavaScript and XML, it is like viagra for your website. Pop in a little Ajax and your website is performing better and making your “visitors” happy. As with viagra you need to read the warning labels associated with Ajax.
Warning:
Ajax should be used in moderation. If your server maintains a full load for more than 4 hours as a result of using Ajax, you should consult a programmer immediately. Websites hosted on shared servers or those intending to move to a shared server should not use Ajax as this may cause complications during deployment.
Seriously though, over use of Ajax can cause a lot of load on your server if you are processing too many requests at the same time, often the requests that tax the server the most include database queries. Don’t get me wrong, I love Ajax and I think every site could put it to good use. This is why I am going to show you a few simple methods to implement it on your site to make your visitors time more enjoyable.
Asynchronous JavaScript and XML, it is a little misleading, you do not really need XML, nor do you need JavaScript, come to think of it, it does not need to be asynchronous either. There have been some releases of API’s which allow you to use Ajax without JavaScript, not really sure why anyone would want to go this route as it is not as powerful. Most commonly we will use XMLHttpRequest to exchange our information.
Our Scenario
This is one of the most useful scenarios I can think of at this particular time, considering the lack of caffeine flowing through my veins I think it is a good one. How many times have you gone to register on a website to find that your user name was taken? Out of those times how many times did you have to submit the form over and over again until you found a user name that was not taken? Through the use of Ajax we can check if a particular user name is available while we type.
This method is very easy, we will start with our php which will query the database and tell us if a particular user name is available.
checkname.php
$result = mysql_num_rows(mysql_query("SELECT * FROM users WHERE username=’$username‘"));
if ($result==0) {
echo ‘Available!’;
} else {
echo ‘Taken!’;
}
The code above simply takes a value “username” which was posted and checks if it already exists in our database. If the username entered exists we echo “Taken”, otherwise we echo “Available”.
So now we need a form and a JavaScript function which will post data to the php script we just created. What we will do is this, every time a key is pressed inside our text input we will call a JavaScript function called “checkname” which will post the value of our input to our checkname.php file.
A simple input box with a call to a JavaScript function. We also added a DIV on the page with the id “results”, this is where we will post the results of our query. All that is left now is the function which will actually gather the data from the page, post it to our php file, then return the results to our DIV.
The Listener
Something almost all women wish men would do, listen… We need to put a listener into place to see if anything is being parsed back to our page. Since browsers are different and handle the data being sent to them differently we need to account for both of them. Here is how:
if (typeof XMLHttpRequest != ‘undefined’) {
return new XMLHttpRequest();
}
try {
return new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
return new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
return false;
}
This function creates an XMLHttp request, which will not only send our data but also receive it. So it is a good talker and listener, who needs that viagra now? O.k. So the final part in this equation is to gather and post the data, we can do that with our function “checkname()”.
var new_request = createXMLHttpRequest();
var username = document.getElementById("username").value
postvalue = ‘username=’ + username;
new_request.open("POST","checkname.php", true);
new_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
new_request.onreadystatechange = function() {
if(new_request.readyState == 4 && new_request.status == 200) {
document.getElementById("result").innerHTML = new_request.responseText;
}
}
new_request.send(postvalue);
}
There it is, the function that will collect and post our data as well as place a reply inside our DIV tag. Here is how it works:
The contents of our DIV is set to the response from our php file, so if it found the user name it would read “Taken” and if the name was not found it would read “Available”. This process takes place every time the user presses a key to input a new name into our input field.
Cleaning Up
As with any good night on viagra or a good night with coding there is always some form of cleanup. The code above will demonstrate how to send and receive via Ajax but it is not complete. We are not escaping any values posted and there is plenty of room for errors when posting erroneous data which means you will need to check your data before sending it to your php file.
Doctors Warning
Over use of Ajax can cause your site to run slow, it is great for the client side but can really bog down your server if too many requests are being made at the same time. Use this tool wisely and try not to overdo it, especially if you are on a budget host.
It has been more than four hours so I am off to seek medical erm…
![[del.icio.us]](http://scriptperfect.com/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://scriptperfect.com/wp-content/plugins/bookmarkify/digg.png)
![[dzone]](http://scriptperfect.com/wp-content/plugins/bookmarkify/dzone.png)
![[Facebook]](http://scriptperfect.com/wp-content/plugins/bookmarkify/facebook.png)
![[Furl]](http://scriptperfect.com/wp-content/plugins/bookmarkify/furl.png)
![[Google]](http://scriptperfect.com/wp-content/plugins/bookmarkify/google.png)
![[LinkedIn]](http://scriptperfect.com/wp-content/plugins/bookmarkify/linkedin.png)
![[MySpace]](http://scriptperfect.com/wp-content/plugins/bookmarkify/myspace.png)
![[Newsvine]](http://scriptperfect.com/wp-content/plugins/bookmarkify/newsvine.png)
![[Propeller]](http://scriptperfect.com/wp-content/plugins/bookmarkify/propeller.png)
![[Reddit]](http://scriptperfect.com/wp-content/plugins/bookmarkify/reddit.png)
![[Slashdot]](http://scriptperfect.com/wp-content/plugins/bookmarkify/slashdot.png)
![[Spurl]](http://scriptperfect.com/wp-content/plugins/bookmarkify/spurl.png)
![[StumbleUpon]](http://scriptperfect.com/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Technorati]](http://scriptperfect.com/wp-content/plugins/bookmarkify/technorati.png)
![[Twitter]](http://scriptperfect.com/wp-content/plugins/bookmarkify/twitter.png)
![[Email]](http://scriptperfect.com/wp-content/plugins/bookmarkify/email.png)
Super Website…
[...] that is the end of this article. Here you’ll find some sites that we think you’ll appreciate, just click the links over[...]…
hello…
you have a great blog here! would you like to make some invite posts on my blog?…
The best sites……
[...] Here is a few very similar information [...]…
Visitor recommendations…
[...]one of our fans recently vouched for the following website[...]……
Great Websites…
[...]here are some links to websites that we link to because we think they are worth taking a look at[...]……
Blogs ou should be reading…
[...]Here is a Great Blog You Might Find Interesting that we Encourage You[...]……
Recent Blogroll Additions……
[...]usually posts some very interesting stuff like this. If you’re new to this site[...]……
Read was interesting, stay in touch……
[...]please visit the sites we follow, including this one, as it represents our picks from the web[...]……
Thumbs up…
I saw this really great post today….
Recent Blogroll Additions……
[...]usually posts some very interesting stuff like this. If you’re new to this site[...]……
Links…
[...]Sites of interest we have a link to[...]……
Websites worth visiting…
[...]here are some links to sites that we link to because we think they are worth visiting[...]……
You should check this out…
[...] Wonderful story, reckoned we could combine a few unrelated data, nevertheless really worth taking a look, whoa did one learn about Mid East has got more problerms as well [...]……
Extra Reading…
[...]we like to honor other sites on the web, even if they aren’t related to us, by linking to them. Below are some sites worth checking out[...]…
Read was interesting, stay in touch……
[...]please visit the sites we follow, including this one, as it represents our picks from the web[...]……
Related……
[...]just beneath, are numerous totally not related sites to ours, however, they are surely worth going over[...]……
Thanks you…
There are some interesting points in time in this article but I donft know if I see all of them center to heart. There is some validity but I will take hold opinion until I look into it further. Good article , thanks and we want more! Added to FeedBur…
Websites worth visiting…
[...]here are some links to sites that we link to because we think they are worth visiting[...]……
This will be a terrific web site, could you be interested in doing an interview regarding just how you developed it? If so e-mail me!…
Very useful cheers, I reckon your readers would certainly want a good deal more articles such as this carry on the excellent work….
Cool sites…
[...]we came across a cool site that you might enjoy. Take a look if you want[...]……
Recommeneded websites…
[...]Here are some of the sites we recommend for our visitors[...]……
[...]Sites of interest we have a link to[...]……
[...]usually posts some very interesting stuff like this. If you’re new to this site[...]……
Tumblr article…
I saw a writer writing about this on Tumblr and it linked to…
Related……
[...]just beneath, are numerous totally not related sites to ours, however, they are surely worth going over[...]……
erotyka…
Zajebiste cipka w filmiku dla doroslych, warto to zobaczyc….
Gems form the internet…
[...]very few websites that happen to be detailed below, from our point of view are undoubtedly well worth checking out[...]……
You should check this out…
[...] Wonderful story, reckoned we could combine a few unrelated data, nevertheless really worth taking a look, whoa did one learn about Mid East has got more problerms as well [...]……
Script Perfect…
Thanks for the post. My spouse and i have generally seen that almost all people are desirous to lose weight simply because wish to look slim plus attractive. On the other hand, they do not continually realize that there are many benefits for you to los…
Superb website…
[...]always a big fan of linking to bloggers that I love but don’t get a lot of link love from[...]……
Related Blogs…..
[...] undoubtedly one of our readers fairly recently suggested the following site [...]…
Click here……
[...] Together with a big butterfly pattern, with ribbons all around the charming image of the relaxing born [...]…
Online Article…
[...]very few websites that happen to be detailed below, from our point of view are undoubtedly well worth checking out[...]…
mule…
[...]These are all the steps for heartburn remedies and be away from this problem[...]…
Related.. Trackback…
[...]the time to read or visit the content or sites we have linked to below the[...]…
Nice……
I saw this really good post today….
Great Blog…
Great weblog here! Additionally your website so much up fast! What host are you using? Can I get your affiliate hyperlink for your host? I want my web site loaded up as quickly as yours lol…
Check this out…
[...] that is the end of this article. Here you’ll find some sites that we think you’ll appreciate, just click the links over[...]……
Websites worth visiting…
[...]here are some links to sites that we link to because we think they are worth visiting[...]……
Cool sites…
[...]we came across a cool site that you might enjoy. Take a look if you want[...]……
Sources…
[...]check below, are some totally unrelated websites to ours, however, they are most trustworthy sources that we use[...]……
Superb website…
[...]always a big fan of linking to bloggers that I love but don’t get a lot of link love from[...]……
Online Article……
[...]The information mentioned in the article are some of the best available [...]……
Looking around…
I like to look around the internet, regularly I will just go to Stumble Upon and read and check stuff out…
important links…
Guys check out these important links…
Blogs you should be reading…
[...]Here is a Great Blog You Might Find Interesting that we Encourage You[...]……
Pleasant…
Such clever work and reporting! Keep up the great work I’ve added your blog to my blogroll. This is a great article thanks for sharing this informative information.. I will visit your blog regularly for some latest post….
studied. lately there is an increase in…
data showing that hormone replacement therapy, htr can increase the risk of coronary artery disease and breast cancer. this has contributed to careful consideration of the risks and benefits of systemic htr. this is the reason that htr is not recommend…
About thatarticle I read……
…check that out – we’re linking to……
Just Browsing…
While I was browsing yesterday I saw a great post concerning…
Check this out…
[...] that is the end of this article. Here you’ll find some sites that we think you’ll appreciate, just click the links over[...]……