Simple AJAX Tutorial & Example

On January 17, 2008, in MD5, by Zoiz

String :


MD5 :



As requested, this article will show you how to create a simple AJAX Application. Before you start, you may test the AJAX Example above ;) You need 3 files to create this.

  1. md5.html
  2. processor.js
  3. getmd5.php

1. md5.html

Is the AJAX Example interface html file.

<html>
<head>
<script src=”processor.js”></script> <!– imports the AJAX script –>
</head>
<body>
<form>
String :
<input type=”text” id=”txt1″
onkeyup=”showHint(this.value)” size=”27″ <!– call the function on the AJAX script –>
maxlength=”32″>
</form>
<p>MD5 : <b><span id=”txtMd5″></span></b></p> <!– show the result –>
</body>
</html>

2. processor.js

This is file stores AJAX script, and sends information to getmd5.php through $_GET request.

var xmlHttp;

function showHint(str) // sends the calculated MD5 Value to the form
{
if (str.length==0)
{
document.getElementById(“txtMd5″).innerHTML=”";
return;
}

xmlHttp=GetXmlHttpObject(); //define xmlHTTP type
if (xmlHttp==null)
{
alert (“Your browser does not support AJAX!”);
return;
}
var url=”http://zoiz.web.id/getmd5.php”; // PHP Script URL that will calculate MD5
url=url+”?q=”+str; // Send value through GET
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open(“GET”,url,true);
xmlHttp.send(null);
}

function stateChanged()
{
if (xmlHttp.readyState==4) //check if the request is completed
{
document.getElementById(“txtMd5″).innerHTML=xmlHttp.responseText; // assign the value
}
}

function GetXmlHttpObject(); // catch your browser type and assign xmlHttp value
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch (e)
{
xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
}
return xmlHttp;
}

3. getmd5.php

Calculate MD5 from the string it recieves.

<?
$txtMd5=$_GET['q'];
if(strlen($txtMd5)>=33){ // make sure that the string not longer than 32 char
echo “Ups”; // display error
}
else
{
echo md5($txtMd5); // Provide the MD5 hash
}
?>

Jobs done, simple and easy isn’t it? :D Any comment and feedback is welcomed (anyway I am not a pro Coder, correct me if I am wrong in this article :P ).

Reference :

http://www.w3schools.com/ajax/

 

11 Responses to “Simple AJAX Tutorial & Example”

  1. eddy says:

    Nice.

    Btw, you might want to glue up the onkeyup event some timeout delay before every key press would eventually call some XHR request. Like typing “testing” chars would brings up 7 XHR calls which isn’t necessarily needed as the the final output would just need one XHR call.

    My 2 cents.

  2. Zoiz says:

    thanks, that’s a nice idea ;)

  3. stu says:

    ok, Zoiz. nice to know you. I has put your site address in my blogroll. It’s ok if you put my site address. Thanks

  4. Zoiz says:

    hehe.. Ok, i’ve also added your blog to my blogroll ;)

  5. Idoenk says:

    ew, i thought it talk `bout a soccer team, >,<
    I think I fall in love with AJAX. :D

    Thanks.
    .::Astalavista::.

  6. M1nD says:

    hi…
    i want to ask s’thing…
    hehe…

    i want to build a ‘md5 encryption’ page like http://zoiz.web.id/ajaxify-md5-encryption
    but my homepage is error… http://m1nd.th0r.info/?page_id=43

    i already install the plugin, but i don’t know how to use it…

    may you teach me??
    i’m only a newbie…
    my e-mail: raymond.tenggario@gmail.com
    hehehe…

    Thankz for all….:)

  7. Zoiz says:

    You miss this one M1nD, remove the / ;)

  8. Sizuki says:

    can you send me that’s script and teach me how to install it….thank’s

  9. Zoiz says:

    You only need three files, and all of the source codes are there :D copy and save them in separate file and execute he md5.html in your browser ;) I am sure it’ll work Sizuki^^

  10. M1nD says:

    Thankz,bro….
    but….

    gw bnr2 g ngerti slhnya dmn….
    plz help me….
    Tq yach…;)

  11. Sree says:

    its really fantasic:)

Leave a Reply