Saturday, May 17, 2008

Simple Ajax Script


Ajax.js


subject_id = '';
function handleHttpResponse()
{
if (http.readyState == 4)
{
if (subject_id != '')
{
document.getElementById(subject_id).innerHTML =
http.responseText;
}
}
}
function getHTTPObject()
{
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try
{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e)
{
try
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch (E)
{
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
{
try
{
xmlhttp = new XMLHttpRequest();
} catch (e)
{
xmlhttp = false;
}
}
return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object

function getScriptPage(div_id,content_id)
{
subject_id = div_id;
content = document.getElementById(content_id).value;
http.open("GET", "server_script.php?content=" + escape(content), true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
}

index.html


<input onkeyup="getScriptPage('output_div','text_content')"
id="text_content" size="40" type="text>

<div id="output_div">
OutPut will get printed here...!
</div>
server_script.php



<?php
print 'Content Typed - '.$_GET['content'];
?>

No comments: