I'm doing a registration form.I want to validate the email address before submitting the form using ajax.That is to verify if the email address already exist in the database.Can anyone help me please?I don't know what's wrong with these code!
regist.html:
<html>
<head>
<script type="text/javascript">
var http_request; //global variable
function makePOSTRequest(url, parameters) {
http_request = false;
/* For Firefox*/
if (window.XMLHttpRequest) {
http_request = new XMLHttpRequest();
} else /*For IE*/
if (window.ActiveXObject) {
try {
/*For some versions of IE*/
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
/*For some other versions of IE*/
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
function AjaxFunction(email)
{
alert(email);
xmlhttp = createXMLHttpRequest() ;
xmlhttp.open("GET","regist_pro.php?email=" + email, true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState = 4)
{
return;
}
document.getElementById("emailtext").innerHTML=xmlhttp.responseText;
};
xmlhttp.send(null);
}
function createXMLHttpRequest() {
try
{
return new XMLHttpRequest();
}
catch(e)
{}
try
{
return new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{}
alert("XMLHttpRequest not supported");
return null;
</script>
</head>
<body>
<table cellpadding="2" height='3'>
<div id="user">
<form method="get" action="regist_pro.php"name="name">
<tr>
<td><font size='2'>First name: </font></td>
<td><input name="firstname" type="text" /></td>
<td><font size='2'>Email Address: </font></td>
<td><input name="email" type="text" onmouseout="javascript:AjaxFunction(this.value);"/></td>
<td><font size='2'>Sex:</font>
<select name="sex">
<option>Female</option>
<option>Male</option>
</td>
</tr>
<tr>
<td><font size='2'>Last name: </font></td>
<td><input name="lastname" type="text" /></td>
<td><font size='2'>Password: </font></td>
<td><input name="password" type="password" /></td>
<td><font size='2'>Birthday: </font></td>
<td>
</tr>
<tr>
<td colspan='2' align='right'><input type="submit" name="submit" value="Register" size='5'/></td>
<td align='right'><input type="reset" name="reset" value="Reset" size='5'/></td>
</tr>
</form>
</div>
<div id="emailtxt">esfs</div>
</table>
</body>
</html>
regist_pro.php:
<?php
session_start();
include ("db_connect.php");
$fname = $_GET['firstname'];
$lname = $_GET['lastname'];
$email = $_GET['email'];
$password = $_GET['password'];
$sex = $_GET['sex'];
//$birthday= $_GET['birthday'];
$Select= "Select * FROM registration Where email='$email'";
$rs=mysql_query($Select);
if (mysql_num_rows($rs) > 0)
{
mysql_close($con);
echo "Email Address already exists.";
}
else{
mysql_query("INSERT INTO registration (firstname, lastname,email,password,sex,birthday)
VALUES ('$fname', '$lname','$email','$password','$sex', '$birthday')");
mysql_close($con);
header("Location: chat.html");
}
?>