<html>
<body>
<script type="text/javascript">
function ajaxFunction(str)
{
var httpxml;
try
{
// Firefox, Opera 8.0+, Safari
httpxml=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
httpxml=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
httpxml=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
///////End of First part- Checking browser support//////
////// Start of receiving data from server //////
function stateChanged()
{
if(httpxml.readyState==4)
{
document.myForm.time.value=httpxml.responseText;
}
}
/////// End of receiving data from server //////
/////// Start of posting data to server ///////
var url="ajax-get-testck.php";
url=url+"?txt="+str;
url=url+"&sid="+Math.random();
httpxml.onreadystatechange=stateChanged;
httpxml.open("GET",url,true);
httpxml.send(null);
}
//////// End of posting data to server ////////
</script>
<form name="myForm">
Name: <input type="text"
onkeyup="ajaxFunction(this.value);" name="username" />
Time: <input type="text" name="time" />
</form>
</body>
</html>
At the server end we have the file ajax-get-testck.php, check this line
var url="ajax-get-testck.php";
The code inside the file ajax-get-testck.php is here.
<?Php
$in=$_GET['txt'];
if(strlen($in) < 20){$in=$in." Welcome ";}
else{$in=" Too Long Data";}
echo $in; // returned Message
?>
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.