我有一张表格需要用户填写。它的用户注册表。我希望当用户填写用户名文本时,我的表单应该搜索用户名,无论它是否存在。如果存在,那么用户名文本字段应该获得焦点。我的数据库表名称是 users,它包含名为 id、用户名、密码的列,地位。 这是我的代码。
<form name="user" action="#" method="post">
<label>Username</label>
<input type="text" maxlength="25" name="username" />
/*here i want to check from my db table weather username exists or not*/
<label>Password</label>
<input type="password" maxlength="25" name="password" />
<input type="submit" value="Submit" />
</form>
我可以在用户提交表单时执行此操作。但我的要求是在不提交表单的情况下检查值,并且用户名应该获得焦点。 在 php 中我有以下代码
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
$username = $_REQUEST['username'];
$query=mysql_query("SELECT * from user where name='$username'");
$row=mysql_num_rows($query);
if($row==1){
echo "true";
} else {
echo "false";
}
?>
最佳答案
向您的服务器端脚本发出 ajax 请求,该脚本会检查数据库并返回您可以解释的内容。然后根据 ajax 成功响应对其进行操作(聚焦该字段)。
网上有数千个这样的例子。查看 @danish hashmi 的示例 ( http://blog.webwizo.com/2011/06/03/check-username-availability-in-php-with-jquery-ajax/ ) 或通过 google 搜索了解更多信息。
关于php - 如何在不发布表单的情况下检查表单中的重复用户名,并将焦点放在用户名(如果已存在)上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13700342/