javascript - Check Username in Registration form -
in following code, want username checked if it's available or not before submiting form. used onchange()
event username field.
html code:
<form method="post" action="regconf.php"> <p> <input type="text" id="username" name="username" value="" placeholder="username" onchange="check_ava()"> <span id='ava_result'> </p> <p> <input type="password" id="password" name="password" value="" placeholder="password"> </p> <p> <input type="password" id="confpassword" name="confpassword" value="" placeholder="confirm password" onchange="check()"> <span id='message'> </p> <p> <input type="text" name="email" value="" placeholder="email"> </p> <p> <input type="text" name="phone" value="" placeholder="phone"> </p> <p> <input type="text" name="address" value="" placeholder="address"> </p> <p class="submit"> <input type="submit" id="submit" name="commit" value="submit"> <button type="reset" value="reset">reset</button> </p> </form>
function check_ava() { var username = document.getelementbyid('username').value; $.post("check_username.php", { username: username }, function(result){ if (result == 1) { document.getelementbyid('ava_result').innerhtml = "username available"; document.getelementbyid('submit').disabled = false; } else { document.getelementbyid('ava_result').innerhtml = "username not available"; document.getelementbyid('submit').disabled = true; } }); }
<?php include 'dbconnection.php'; $username = mysql_real_escape_string($_post['username']); $result = mysql_query('select name user name = "'. $username .'"'); if (mysql_num_rows($result) > 0) { echo 0; } else { echo 1; } ?>
the problem is not working , not checking username.
you have use remote method:
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.min.js"></script> <script> $(function() { $('#form-validation').validate({ rules: { username: { required: true, remote: { url: "check_username.php", type: "post" } }, }, messages: { val_number: 'please enter value!', } }); }); </script>
and check_username.php is,
$registeredname[] = //select names in databse using select , store here array, $requestedname = $_post['username']; if(in_array($requestedname,$registeredname)) { echo 'false'; } else { echo 'true'; }
Comments
Post a Comment