Php 使用ajax检查可用性
我想检查用户使用ajax在表单中键入的“电子邮件”的可用性。所以我写了下面的代码,但什么也没发生 我的错误在哪里 html文件:Php 使用ajax检查可用性,php,ajax,Php,Ajax,我想检查用户使用ajax在表单中键入的“电子邮件”的可用性。所以我写了下面的代码,但什么也没发生 我的错误在哪里 html文件: <form id="frmSignUp" name="frmSignUp" method="post" action="signup.php"> <!-- Some input fields here--> <p> <label for="email">You
<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>
您的电子邮件:
document.getElementById(“email”).onblur=function(){
var-xmlhttp;
var uname=document.getElementById(“电子邮件”);
如果(email.value!=“”){
if(window.XMLHttpRequest){
xmlhttp=新的XMLHttpRequest();
}
否则{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“status”).innerHTML=xmlhttp.responseText;
}
};
xmlhttp.open(“GET”,“/email\u availability.php?email=“+encodeURIComponent(email.value),true);
xmlhttp.send();
}
};
电子邮件_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>";
}
?>
使用disabled属性禁用提交…并停止使用mysql系列..使用mysqli或PDO从email='{$email}'的用户中选择id
这可以是从email='$email'的用户中选择id
@susheel测试此项但无任何更改任何控制台错误,并查看网络选项卡中的响应…@susheel no。。我没有错。。。