I am trying to make a simple AJAX interaction where users type an email address into a form and when the from reaches ready state 4, the embedded java script makes a call to the server to run a PHP regexp check on the string submitted by the user, validating that it is in the right email format.
I don't really know what I am doing, and my method has been basically to grope blindly forward utilizing various internet tutorials.
However, it seems to me that what I have should work, but of course it doesn't.
so here's the html framework up to the point of interest (the double email field for validation) with the javascript snippet located in the head
:
Code:
<script language="javascript" src="checkemail.js"></script>
Code:
<form id="register" method="post" action="userdatabase.php" name="register">
<p><label>First Name: </label><input type="text" name="fname" class="text"/></p>
<p><label>Last Name: </label><input type="text" name="lname" class="text"/></p>
<p><label>Your Email: </label><input type="text" name="email" id="email" onChange="AjaxFunction();" class="text"/>
<div id="rsp_email"><!----></div>
<p><label>Re-enter Email: </label><input type="text" name="email2" class="text"/></p>
Here is the actual javascript 'checkemail.js':
Code:
<script type="text/javascript">
function AjaxFunction(email)
{
var AjaxRequest;
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;
}
}
}
ajaxRequest.onreadystatechange= function()
{
if(readystate==4)
{
document.register.email.value = ajaxRequest.ResponseText;
}
ajaxRequest.open("GET","emailcheck.php", true);
ajaxRequest.send(null);
}
}
and finally here is the php that the js utilizes- 'emailcheck.php':
PHP Code:
if(isset(!''){
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
echo "<center>Invalid email</center>";
}else{
echo "<center>Valid Email</center>";}
}
I am sure I have lost the plot somewhere within this, as I am still struggling to conceptualize all the threads being passed around. but syntactically I thought I was doing ok, but apparently not.
Please help me figure out what the problem is somebody... anybody.
I am running this through my godaddy hosting, and MAMP to see if it works.
brian