Wednesday, April 7, 2010

Ajax

Ajax Validation


Are you encountered the problems how to validate existing emails,usernames in the system? Useing ajax we can validate system existing data in easy. Here the sample code for validate system existing data without page submiting.


Copy this code and save as ajax-validation.php


<head>
<script type="text/javascript">
function getHTTPObject(){
if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest) return new XMLHttpRequest();
else {
alert("Your browser does not support AJAX.");
return null;
}
}

function check_web(){
httpObject_web = getHTTPObject();
if (httpObject_web != null) {
httpObject_web.open("GET", "check.php?web="+document.getElementById('web').value, true);
httpObject_web.send(null);
httpObject_web.onreadystatechange = setOutput_web;
}
}
//Set out put
function setOutput_web(){
if(httpObject_web.readyState == 4){
document.getElementById('message_web').innerHTML = httpObject_web.responseText;
}
}

var xmlHttp

function GetXmlHttpObject(){
var xmlHttp=null;
try{
xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari
}
catch (e){
try{// Internet Explorer
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
var httpObject = null;
</script>
</head>

<body>
Type Web Address<input name="web" id="web" size="30" type="text" onkeyup="check_web();" /><label id="message_web" style="color:#FF0000"></label>
</body>




Copy This and save as check.php

<?
$web = 'http://studyeasyphp.blogspot.com';
if($_REQUEST['web'] == $web){
echo "This web address is already exist in our database";
} else {
echo "Ok";
}
?>