检查用户名是否可用AJAX [英] Checking if the username is available with AJAX
问题描述
我使用的AJAX基本登记表,但形式不能连接到数据库。我显然忽视的东西。
I'm using a basic registration form with AJAX, but the form is not connecting to the database. I'm obviously overlooking something.
因此,这里是我想要验证的字段。
So here's the field I want to validate.
Username:<input type="text" name="user" id="user" maxlength="30">
<span id="msgbox" style="display:none"/></input>
然后我使用jQuery,这里的code:
Then I use jQuery, here's the code:
$(document).ready(function() {
$("#user").blur(function() {
//remove all the class add the messagebox classes and start fading
$("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
//check the username exists or not from ajax
$.post("user_availability.php",{ user_name:$(this).val() },
function(data) {
if(data=='no') { //if username not avaiable
$("#msgbox").fadeTo(200,0.1,function() {//start fading the messagebox
//add message and change the class of the box and start fading
$(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
});
} else {
$("#msgbox").fadeTo(200,0.1,function() { //start fading the messagebox
//add message and change the class of the box and start fading
$(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);
});
} // else
} // function
); // $.post
}); // blur
}); // ready
和我有这个code, user_availability.php
:
And I have this code, user_availability.php
:
mysql_connect(localhost,$user,$password);
or die('There is error to connect to server:-'.mysqli_connect_error());
$db_selected = mysql_select_db($database);
$user_name=$_POST['user_name'];
$sql = "select * from members where username='$user_name'";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))
{
$existing_users[] = $row['username'];
}
if (in_array($user_name, $existing_users))
{
echo "no"; //user name is not availble
}
else
{
echo "yes"; //user name is available
}
我没有得到任何数据库错误。该形式将更加可观,但我不能把它与这个领域的工作。有什么建议?
I get no database errors. The form will be more substantial, but I can't get it to work with this field. Any suggestions?
推荐答案
SELECT语句只是鸡蛋里挑骨头。因为你只需要获得多少行所输入的用户名存在,你不需要做一个SELECT *为依据列在用户表,你可能会返回相当多的数据的数量。我将修改您的查询要像这样:
Just a nitpick on the select statement. Since you only need to get how many rows exist for the entered username you don't need to do a "select *" as depending on the number of columns in your users table you could be returning quite a bit of data. I would revise your query to be like so:
$sql = "select COUNT(username) from members where username=$user_name";
然后检查,以确保查询的结果等于零。如果是这样,则用户名是可用的。
Then you check to make sure that the result of the query equals zero. If it does then the username is available.
我知道上面是不是一个回答你的问题,但只是想我要指出来,因为这看起来是,是要被调用了很多依赖于流量的报名表和创意功能您的访客。
I know that the above wasn't an answer to your question but just thought I would point it out since this looks to be a function that is going to get called a lot depending on the traffic on your registration form and the creativity of your visitors.
这篇关于检查用户名是否可用AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!