我的PHP代码可以很好地验证电子邮件,但对于用户名,它会一直返回false
。我只是想知道如何解决它?
PHP:
<?php
include_once("db_connect.php");
// USERNAME
if(!empty($_POST['username'])) {
$username = $mysqli->real_escape_string($_POST['username']);
$query = "SELECT * FROM users WHERE username = '$username'";
$results = $mysqli->query($query);
if($results->num_rows == 0)
{
echo "true"; //good to register
}
else
{
echo "false"; //already registered
}
} else {
echo "false";
}
?>
Html表格
<div id="registerForm">
<h1>Register</h1>
<form action="" method="POST" class="registerForm form-horizontal">
<div class="form-group">
<label class="control-label col-sm-2" for="username" id="username">Username:</label>
<div class="col-sm-10">
<input type="text" name="username" class="form-control" placeholder="Enter a username">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="email">Email:</label>
<div class="col-sm-10">
<input id="email" type="email" name="email" class="form-control" placeholder="Enter your email - IMPORTANT">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="password">Password:</label>
<div class="col-sm-10">
<input id="password" type="password" name="password" class="form-control" placeholder="Enter a password">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="confirmpassword">Confirm Password:</label>
<div class="col-sm-10">
<input id="confirmpassword" type="password" name="confirmpassword" class="form-control" placeholder="Confirm password">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="code">Code:</label>
<div class="col-sm-10">
<input type="text" id="code" name="code" class="form-control" placeholder="Enter Register Code">
</div>
</div>
<button type="submit" class="btn btn-primary">Register</button>
</form>
</div>
和jquery:
$(document).ready(function () {
$('.registerForm').validate({
onkeyup: false,
rules: {
username: {
required: true,
rangelength: [5, 20],
remote: {
url: 'core/check2.php',
type: "post",
data:
{
username: function() {
return $( "#username" ).val();
}
}
}
},
password: {
required: true,
rangelength: [5, 20],
},
confirmpassword: {
required: true,
rangelength: [5, 20],
equalTo: password
},
email: {
required: true,
remote: {
url: 'core/check.php',
type: "post",
data: {
email: function() {
return $( "#email" ).val();
}
}
}
},
code: {
required: true,
remote: {
url: 'core/checkcode.php',
type: "post",
data: {
code: function() {
return $( "#code" ).val();
}
}
}
}
},
messages: {
username: {
required: "Enter a username.",
rangelength: "Username must be 5 to 20 character",
remote: "Username have been taken"
},
password: {
required: "Enter a password.",
rangelength: "Password must be 5 to 20 character",
remote: "Email have been taken"
},
confirmpassword: {
required: "Confirm your password.",
rangelength: "Password must be 5 to 20 character",
equalTo: "Your password does not match"
},
email: {
required: "Enter your email(NOT SPAM EMAIL).",
remote: "This Email have been taken"
},
code: {
required: "Enter your Register Code",
remote: "Invalid Code"
}
}
});
});
答案 0 :(得分:1)
<input id="username" type="text" name="username" class="form-control" placeholder="Enter a username">
你错过了id标识符,这都是
答案 1 :(得分:0)
您id="username"
和label
重复了input
。那你的问题。