douchuang4181 2015-04-25 12:15
浏览 71

在注册表中检查用户名

In the following code, I want the username to be checked if it's available or not before submiting the form. I used onchange() event with the 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 is available";
            document.getElementById('submit').disabled = false;
        } else {  
            document.getElementById('ava_result').innerHTML = "Username is not available";
            document.getElementById('submit').disabled = true;
        }  
    });  
} 
<?php
    include 'DBConnection.php';

    $username = mysql_real_escape_string($_POST['username']);  
    $result = mysql_query('select Name from User where Name = "'. $username .'"');  
    if (mysql_num_rows($result) > 0) {  
        echo 0;  
    } else {  
        echo 1;  
    }
?>

The problem is that it is not working and not checking the username.

  • 写回答

1条回答 默认 最新

  • dongyao8698 2015-04-25 12:34
    关注

    You Have to 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 a Value!',
    
                        }
                    });  
                });
    </script>
    

    and your check_username.php is,

    $registeredName[] = //select all names in your databse using select and store here as array, 
    
    $requestedName  = $_POST['username'];
    if(in_array($requestedName,$registeredName))
    {
        echo 'false';
    }
    else
    {
        echo 'true';
    }
    
    评论

报告相同问题?