AJAX用户名可用性 [英] AJAX username Availability
问题描述
我已编写此代码来检查电子邮件的可用性.
I have writtin this code to check the email availability.
var email = $('#email_reg').val();
if(email && email.length > 0)
{
if(!isValidEmailAddress(email))
{
isValid = false;
$('#msg_email').html('Email is invalid').show();
}
else
{jQuery.ajax({
type: 'POST',
url: 'check_username.php',
data: 'email='+ email ,
cache: false,
success: function(response){
if(response == 1){
$('#msg_email').html('Email already Exists').show();
isValid=false;
}
else {
$('#msg_email').html('').hide();
}
}
});
}
}
else
{
isValid = false;
$('#msg_email').html('Please enter email').show();
}
PHP代码为
<?php
require_once('Connections/connection.php');
$username= mysql_real_escape_string($_REQUEST["email"]);
if (!$con)
{
echo 0;
}
else {
mysql_select_db($database_connection, $connection);
$result = mysql_query("SELECT * FROM vendor_logiin WHERE username='" . $username . "'");
$num = mysql_num_rows($result);
echo $num; //it will always return 1 or 0 since we do not allow multiple users with the same user name.
}
mysql_close();
?>
现在所有其他功能都可以正常工作,就像将其留空并提供错误的电子邮件格式一样.但是问题是当我提供已经存在的电子邮件ID时.它没有给出错误.我不知道出了什么问题.
Now all the others work well like when left it empty and give a wrong email format.But the problem is when i give an email Id that already exists. It didnot give error. I have no idea what is going wrong.
推荐答案
由于您未指定 dataType
,因此响应可能被视为文本或html,在这种情况下,这样做可能是明智的选择比较形式为字符串:
Since you didn't specify dataType
the response is probably treated as text or html and in that case it might be wise to do the comparison as a string:
if (response == "1") {...}
而不是数字.或使用 parseInt(response,10)== 1
(如果将其与数字进行比较).
instead of a number. Or use parseInt(response, 10) == 1
if you compare it as a number.
这篇关于AJAX用户名可用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!