我希望能够在提取用户名时禁止我的表单提交,我希望得到一些帮助。
我的js代码是:
$('#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('');
}
});
我的检查用户名是否存在:
<?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');
}
}
?>
答案 0 :(得分:0)
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
}
});
希望它有所帮助!