使用ajax检查用户名是否存在 [英] check username exists using ajax

查看:117
本文介绍了使用ajax检查用户名是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码检查用户名是否存在于数据库中.代码运行良好,并显示可用或采用的用户名.现在,当用户选择之前使用的用户名时,我想提交按钮应该被禁用;当用户名可用时,应启用按钮.请指导我.

I use this code to check username exists in database before or not. code works good and shows available or taken username. now i want to submit button should be disable when user select username that was taken befor and enable when username available . please guide me how.

$(document).ready(function() {
    $('#username').keyup(function() {
        $.post('adm/chk_uname_avail.php', { 
            uname : changeuser.username.value 
        }, function(result){
            $('#available').html(result);
        })
    })
})

推荐答案

我正在使用旧的$.ajax函数,并确保在adm/chk_uname_avail.php上具有布尔类型的键键入taken的数据(例如),并且请注意,您应该从中返回JSON数据类型.

I'm using the old $.ajax function and make sure you have a data keyed taken (as example) with boolean type on adm/chk_uname_avail.php and notice that you should return JSON data type from it.

adm/chk_uname_avail.php

<?php
//return response as JSON
header('Content-type:application/json;charset=utf-8');

....
....
....

$data['taken'] = true; //show this response to ajax
echo json_encode($data);

?>

Ajax

$(document).ready(function() {
   $('#username').on('keyup', function() {
        $.ajax({
            type: 'POST',
            url: 'adm/chk_uname_avail.php',
            data: {uname : changeuser.username.value},
            success: function(result) {
                var $btn = $('#submiButton');
                if (result.taken) {
                    $btn.prop('disabled', true);
                } else {
                    $btn.prop('disabled', false);
                }
                //As @Mikey notice, You can just use this as simply as
                //$('#submiButton').prop('disabled', result.taken);
            }        
        });
   });
});

这篇关于使用ajax检查用户名是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆