我认为这可能是我的代码无法工作的原因..'if(name.val()。length> 3)'永远不能执行所以我发出警告来测试返回的数据,这就是它的样子:
我的js
$(document).ready(function(){
var form = $("#customForm");
var name = $("#name");
var nameInfo = $("#nameInfo");
var email = $("#email");
var emailInfo = $("#emailInfo");
var pass1 = $("#pass1");
var passInfo = $("#pass1Info");
var pass2 = $("#pass2");
var pass2Info = $("#pass2Info");
var state = false;
name.keyup(validateName);
function validateName(){
if(name.val().length <= 3){
name.removeClass("valid");
nameInfo.removeClass("valid");
name.addClass("error");
nameInfo.addClass("error");
nameInfo.text("Minimum 4 characters!");
state = false;
}else{
if(name.val().length > 3){
var username=name.val();
$.post('validate.php',{names: username},function(data){
alert(data);
if(data!=0){
name.removeClass("valid");
nameInfo.removeClass("valid");
name.addClass("error");
nameInfo.addClass("error");
nameInfo.text("The username is already taken!");
state = false;
}else{
name.removeClass("error");
nameInfo.removeClass("error");
name.addClass("valid");
nameInfo.addClass("valid");
nameInfo.text("username available");
state = true;
}
});
}
}
}
return state;
//end
});
我的PHP代码:
<?php
$name = $_POST['names'];
$email = $_POST['emails'];
if($name !=""){
mysql_connect("localhost","root","") or die("Fail to connect to database");
mysql_select_db("reglog");
$uname = mysql_query("SELECT username FROM users WHERE username='$name'");
$count = mysql_num_rows($uname);
if($count !=0){
echo 1;
}else{
echo 0;
}
}
if($email !=""){
mysql_connect("localhost","root","") or die("Fail to connect to database");
mysql_select_db("reglog");
$useremail = mysql_query("SELECT email FROM users WHERE email='$email'");
$countemail = mysql_num_rows($useremail);
if($countemail !=0){
echo 1;
}else{
echo 0;
}
}
?>
答案 0 :(得分:1)
投掷warning
。养成检查数组中index
是否可用的习惯,从而消除此类错误的可能性。
$email = isset($_POST['emails']) ? $_POST['emails'] : '';
或不显示任何错误以禁止此类警告(不推荐)。
正如Kai所说,你没有传递任何变量emails
。
$.post('validate.php',{ names: username, 'emails' : email }, ... }
答案 1 :(得分:0)
您不能将电子邮件作为帖子变量传递。
$.post('validate.php',{names: username},function(data){
应该是
$.post('validate.php',{names: username, emails: email},function(data){
AS starx说,在对变量
运行进程之前,应该使用isset检查post值答案 2 :(得分:0)
将返回类型设为html
,它会起作用。