我想检查用户在带有ajax的表单中输入的“电子邮件”的可用性。所以我写了下面的代码,但没有发生!
我的错误在哪里?
html文件:
<form id="frmSignUp" name="frmSignUp" method="post" action="signup.php">
<!-- Some input fields here-->
<p>
<label for="email">Your email:</label>
<input name="email" class="text" id="email" /> <span id="status"> </span>
</p>
<p>
<input type="submit" name="Submit" value="Submit" id="submit" />
</p>
<br />
</form>
<script type="text/javascript">
document.getElementById("email").onblur = function() {
var xmlhttp;
var uname=document.getElementById("email");
if (email.value != "") {
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("status").innerHTML=xmlhttp.responseText;
}
};
xmlhttp.open("GET","/email_availability.php?email="+encodeURIComponent(email.value),true);
xmlhttp.send();
}
};
</script>
email_availability.php包含:
<?php
require("_includes/constants.php");
$email=$_REQUEST['email'];
if( preg_match("/[^a-z0-9]/",$email) ) {
print "<span style=\"color:red;\">Username contains illegal charaters.</span>";
exit;
}
// 1. Create a database connection
$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
mysql_set_charset('utf8',$connection);
if (!$connection) {
die("Database connection failed: " . mysql_error());
}
// 2. Select a database to use
$db_select = mysql_select_db(DB_NAME,$connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
$query = "SELECT id FROM users WHERE email = '{$email}'";
$db_result = mysql_query($query, $connection);
$record = mysql_fetch_array ( $db_result );
if( mysql_num_rows($db_result)>0 ) {
print "<span style=\"color:red;\">Username already exists :(</span>";
}
else {
print "<span style=\"color:green;\">Username is available :)</span>";
}
?>
我有另一个问题:
在我的注册表单中,如果电子邮件不可用,如何冻结提交按钮?!
请帮助我的朋友......谢谢