php - disable button on ajax username request -
i have jquery check if username in use before submitting registration form. username check works fine. however, want disable submit button if username not-available. code. works first time: if username unavailable button disabled. once username unavailable username after available still leaves submit button disabled. how can button become enabled without having refresh page?
<script type="text/javascript"> $(document).ready(function() { $("#username").keyup(function (e) { //removes spaces username $(this).val($(this).val().replace(/\s/g, '')); var username = $(this).val(); if(username.length < 4){$("#user-result").html('');return;} if(username.length >= 4){ $("#user-result").html('<img src="imgs/ajax-loader.gif" />'); $.post('check_username.php', {'username':username}, function(data) { if(data == '<img src="imgs/available.png" /> username available'){ $("#sumbit").attr("disabled", false); $("#user-result").html(data); }else{ $("#submit").attr("disabled", true); $("#user-result").html(data); } }); } }); }); </script>
check_username.php looks this:
//sanitize username $username = filter_var($username, filter_sanitize_string, filter_flag_strip_low|filter_flag_strip_high); //check username in db $results = mysqli_query($connecdb,"select username users username='$username'"); //return count $username_exist = mysqli_num_rows($results); if($username_exist) { die('<img src="imgs/not-available.png" /> username not available'); }else{ die('<img src="imgs/available.png" /> username available'); }
html submit looks this: html input is
<input type="submit" id="submit" class="special" name="submit" value="register" />
try remove disable using : $("#sumbit").removeattr("disabled");
try code
<script type="text/javascript"> $(document).ready(function() { $("#username").keyup(function (e) { //removes spaces username $(this).val($(this).val().replace(/\s/g, '')); var username = $(this).val(); if(username.length < 4){$("#user-result").html('');return;} if(username.length >= 4){ $("#user-result").html('<img src="imgs/ajax-loader.gif" />'); $.post('check_username.php', {'username':username}, function(data) { if(data == 'available'){ $("#submit").attr("disabled", false); $("#user-result").html('<img src="imgs/available.png" /> username available'); }else{ $("#submit").attr("disabled", true); $("#user-result").html('<img src="imgs/not-available.png" /> username not available'); } }); } }); }); </script>
check_username.php looks this:
//sanitize username $username = filter_var($username, filter_sanitize_string, filter_flag_strip_low|filter_flag_strip_high); //check username in db $results = mysqli_query($connecdb,"select username users username='$username'"); //return count $username_exist = mysqli_num_rows($results); if($username_exist) { die('notavailable'); }else{ die('available'); }
Comments
Post a Comment