Ajax Implementation

5. Implementation Code

Server script : “returnID.php”



$id = $_POST["id"];

//create response for client

print ‘The id received by server is : ‘.$id;

?>

Javascript :

var xmlhttp = false;

function displayID(id){

// for mozilla and safari

if(window.XMLHttpRequest) {

try {

xmlhttp = new XMLHttpRequest();

} catch(e) {

xmlhttp = false;

}

// for IE windows

} else if(window.ActiveXObject) {

try {

xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

} catch(e) {

try {

xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

} catch(e) {

xmlhttp = false;

}

}



// alert(xmlhttp);

//synchronous comm.

xmlhttp.open('POST',"returnID.php",false);

xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

xmlhttp.send("id="+id);

if(xmlhttp.readyState==4){

alert("Text from Server : "+xmlhttp.responseText);

}

}

}

HTML :



No comments: