dongtao9887 2015-01-02 20:47
浏览 10
已采纳

当用户名被占用时,禁用按钮jquery

I would like to be able to have my form disabled from submitting when username is taken, and I would like to get some help with that.

my js code is:

$('#username').keyup(function() {
var username = $(this).val();

$('#username_status').html('Searching...');

if (username !='') {
    $.post('username_check.php', { username: username}, function(data) {
        $('#username_status').html(data);
    });
} else{
    $('#username_status').html('');
}
});

and my for checking username exists or not:

<?php
if(isset($_POST["username"]))
{
define("HOST","localhost");
define("USERNAME","root");
define("PASS","");
define("DBNAME","testingproject");

$connecDB = mysqli_connect(HOST, USERNAME, PASS, DBNAME)or die('could not connect to database');


$username =  $_POST["username"];


$results = mysqli_query($connecDB,"SELECT id FROM attendant WHERE username='$username'");

$username_exist = mysqli_num_rows($results); //records count


if($username_exist) {
  $output= "Sorry, this Username is taken";
  echo ($output);
}else{
  echo('available');
 }
}
?>
  • 写回答

1条回答 默认 最新

  • duanlu6114 2015-01-02 21:11
    关注

    on php page

    if($username_exist) { echo 'yes'; }
    else { echo 'no'; }
    

    on js,

    $.post('username_check.php', { username: username}, function(data) {
        if (data == 'yes') {
          // here disable the registration button
        } else {
          // active the registration button
        }
    });
    

    hope it help!

    also you can check this thread: JQuery/Ajax Post returned data and IF function

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux下点对点协议连接2个USB串口的硬件流量控制问题
  • ¥15 SQL数据自动生成问题
  • ¥15 设密码后无法访问他人的共享文件
  • ¥15 浏览器打不开学校网页
  • ¥100 android开发打不开相册,不弹出授权信息,请问怎么解决?
  • ¥15 我想通过python处理sybase IQ库的一些数据,但是不知道sybase IQ库怎么连接,之前连接都是用linux上的 dbisql
  • ¥15 安装llama-cpp-python==0.1.83失败
  • ¥20 管道轴向耦合水击问题
  • ¥60 补全networkx TODO部分。
  • ¥15 有内推吗,云计算linux运维方向