Ajax Email validator :) AJAX PHP
November 17th, 2008 by adminI think the most important aspects of building and processing a form is how you validate data here is a script build via AJAX and PHP with example how to check if your email address is real one :
this is HTML part :
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; ” />
<title>Email Validator AJAX and PHP</title>
<script language=”JavaScript” type=”text/javascript”>
var request;
/**
* Load XMLDoc function
* here you need put your backend-script url
*/
function doLoad(url) {
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
request.onreadystatechange = processRequestChange;
request.open(“GET”, url, true);
request.send(null);
} else if (window.ActiveXObject) {
request = new ActiveXObject(“Microsoft.XMLHTTP”);
if (request) {
request.onreadystatechange = processRequestChange;
request.open(“GET”, url, true);
request.send();
}
}
}
/**
* Event on request change
*/
function processRequestChange() {
document.getElementById(“resultdiv”).style.display = ‘none’;
abortRequest = window.setTimeout(“request.abort();”, 10000);
// if all is ok
if (request.readyState == 4) {
clearTimeout(abortRequest);
if (request.status == 200) {
document.getElementById(“resultdiv”).style.display = ‘block’;
document.getElementById(“responseHTML”).innerHTML = request.responseText;
} else {
alert(“Sorry can’t receive your dates :n” + request.statusText);
}
}
}
</script>
</head>
<body>
<input type=”text”
id=”emails”
value=”Enter your email pls:”
onFocus=”this.value=”; document.getElementById(‘resultdiv’).style.display=’none’;”/>
<input type=”button”
value=”send”
onClick=”doLoad(‘q.php?email=’+document.getElementById(‘emails’).value);”/><br /><br />
<div id=”resultdiv” style=”display: none;”>
Results:
<span id=”responseHTML”></span>
</div>
</body>
</html>
now we need a php script we will call it q.php:
<?php
/**
* here we receive a headers
*/
header(“Content-type: text/plain; charset=utf-8″);
header(“Cache-Control: no-store, no-cache, must-revalidate”);
header(“Cache-Control: post-check=0, pre-check=0″, false);
$emailw = $_GET["email"];
if (!preg_match(“/^(?:[a-z0-9]+(?:[-_]?[a-z0-9]+)?@[a-z0-9]+(?:[-_.]?[a-z0-9]+(?:[-_]?[a-z0-9]+))?\.[a-z]{2,5})$/”,trim($emailw))){
echo “Pls enter correct email”;
exit;
}
else
{
$email_arr = explode(“@” , $emailw);
$host = $email_arr[1];
if (!getmxrr($host, $mxhostsarr))
{
echo “We try to find $host but can’t .Can you enter another email or check this one pls $emailw”;
exit;
}
getmxrr($host, $mxhostsarr, $weight);
echo “Congratulations your mail is valid : $emailw <br>”;
}
?>
that’s all
if you have any questions pls let me konow Branding-Studio.com


