[英]jquery validate - remote always returns true
I have a form and I am using jQuery validate plugin.我有一个表单,我正在使用 jQuery 验证插件。
I am also using remote to check if the username and email are available or not but I always get false back from the script.我还使用远程检查用户名和电子邮件是否可用,但我总是从脚本中得到错误信息。
here is the jquery validation part:这是jquery验证部分:
$(document).ready(function () {
$('#registration').validate({
rules: {
email: {
email: true,
remote:{
url: "./php/checkemail.php",
type: "POST",
}
},
username: {
minlength: 4,
maxlength: 16,
remote: {
url: "./php/checkusername.php",
type: "POST",
}
},
messages: {
email: {
email: "Please enter a valid e-mail address.",
remote: "This email is already registered.",
},
username: {
minlength: "Your username must be at least 4 characters long but less than 16",
maxlength: "Your username must be at least 4 characters long but less than 16",
remote: "This username is already registered.",
},
submitHandler: function(form) {
$('#registration').ajaxSubmit();
return false;
},
});
});
and here is the checkemail.php file:这是 checkemail.php 文件:
$mysqli = mysqli_init();
if(isset($_POST['email'])) {
$email = mysqli_real_escape_string(strtolower($_POST['email']));
$check= mysqli_query("SELECT * FROM users WHERE LOWER email = '$email'");
if(mysqli_stmt_num_rows($check)!=0) {
echo json_encode(false);
} else {
echo json_encode(true);
}
}
Can anyone tell me please what I did wrong and how to fix it?谁能告诉我我做错了什么以及如何解决它?
Also, even if I change the script to send me true, the remote message from the validation script doesn't appear on my form.此外,即使我更改脚本以向我发送 true,来自验证脚本的远程消息也不会出现在我的表单上。
I think the remote function is checking for a string.我认为远程函数正在检查字符串。
So try this:所以试试这个:
if(mysqli_stmt_num_rows($check)!=0) {
echo 'false';
} else {
echo 'true';
}
So, the problem was that the php i used was deprecated and when i changed it to the new version i did it wrong... so the correct way of doing it is this:所以,问题是我使用的 php 已被弃用,当我将其更改为新版本时,我做错了……所以正确的做法是这样的:
if(isset($_POST['email']))
if(isset($_POST['email']))
{
{
$email = mysqli_real_escape_string($link, $_POST['email']);
$email = mysqli_real_escape_string($link, $_POST['email']);
$check= mysqli_query($link, "SELECT * FROM users WHERE email = '".$email."'") or die(mysqli_error($link));
$check= mysqli_query($link, "SELECT * FROM users WHERE email = '".$email."'") or die(mysqli_error($link));
if(mysqli_num_rows($check)!=0)
if(mysqli_num_rows($check)!=0)
{
{
echo 'false';
}else{
}别的{
echo 'true';
}
}
}
}
mysqli_close($link);
mysqli_close($link);
?>
?>
where $link is the connection to the mysql database :其中 $link 是到 mysql 数据库的连接:
$link = mysqli_connect("$db_host", "$db_username", "$db_password", "$db_database") or die ("Error " . mysqli_error($link));
$link = mysqli_connect("$db_host", "$db_username", "$db_password", "$db_database") 要么死 ("Error " . mysqli_error($link));
and now it works just fine.现在它工作得很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.