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 ubuntu20.04怎么安装yolov5(已经安装anaconda)
  • ¥100 基于delphi10.2几个功能的程序demo
  • ¥15 TinyMCE如何去掉自动弹出的“链接…”工具?
  • ¥15 微信支付转账凭证,如何解决
  • ¥15 在win10下使用指纹登录时,界面上的文字最后一个字产生换行现象
  • ¥20 使用AT89C51微控制器和MAX7219驱动器来实现0到99秒的秒表计数,有开始和暂停以及复位功能,下面有仿真图,请根据仿真图来设计c语言程序
  • ¥15 51单片机 双路ad同步采样
  • ¥15 使用xdocreport 生成word
  • ¥15 请教怎么用MATLAB求坐标
  • ¥15 gradle整合es,在创建索引时报错