使用ajax和jquery进行验证

时间:2016-02-10 18:40:06

标签: php jquery sql ajax

这是我迄今为止所做的。

<span id="error_para">*</span> Username:<input type="text" name="field_username" oninvalid="setCustomValidity('Please Enter Unique Username.');" oninput="setCustomValidity('');" required id="username"  />&nbsp;&nbsp;

 <input type='button' id='check_username_availability' name="button1" value='Check Availability'><br />  
 <br />
 <div id='username_availability_result'></div>  <br />

<script>

$(document).ready(function() {  

    //the min chars for username  
    var min_chars = 3;  

    //result texts   
    var checking_html = 'Checking...';  

    //when button is clicked  
    $('#check_username_availability').click(function(){  
        //run the character number check  
        if($('#username').val().length < min_chars){  
            //if it's bellow the minimum show characters_error text '  
            $('#username_availability_result').html(characters_error);  
        }else{  
            //else show the cheking_text and run the function to check  
            $('#username_availability_result').html(checking_html);  
            check_availability();  
        }  
    });  

 });  
//function to check username availability  
function check_availability(){  

    //get the username  
    var username = $('#username').val();  

    //use ajax to run the check  
    $.post("validateusername.php", { username: username }, 
        function(result){  
            //if the result is 1  
            if(result == 1 ){  
                //show that the username is available  
                $('#username_availability_result').html(username + ' is Available');  
            }else if(result == 0){  
                //show that the username is NOT available  
                $('#username_availability_result').html(username + ' is not Available');  
            }else{  
                //show that the username is NOT available  
                $('#username_availability_result').html(username + ' is not procced');  
            }  

    });  

但它只显示用户名没有继续。我认为使用我的php文件进行数据传输存在问题。

我的validateusername.php文件:

<?php


if( isset( $_POST['button1'] ) )
{
//get the username  
$username = mysqli_real_escape_string($_POST['username']);  

$mysqli = new mysqli("localhost", "jigar", "admin", "demo1");

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

$query = mysqli_query($mysqli, "SELECT username FROM logindata WHERE     username= '$username' " ) or die(mysqli_error($mysqli));

if (mysqli_num_rows($query) > 0){
    echo "0";  
} else {   
    echo "1";   
}

$mysqli->close();
}   
?>

2 个答案:

答案 0 :(得分:1)

这不是经过测试的代码,但您可以尝试代替:

if (mysqli_num_rows($query) > 0) {
    echo "0";  
} else {  
    echo "1";      
}

此:

while($row = mysqli_fetch_array($query) {
    if($row['username'] == $username) {
        echo 1;
        $found = 1;
    }
}

if(!isset($found)) { echo 0; }

答案 1 :(得分:1)

在你的ajax中,你只是在ajax数据中发送用户名:

$.post("validateusername.php", { username: username }, 

在PHP中你正在使用

if( isset( $_POST['button1'] ) )

您的ajax请求不包含此内容。它应该是

if( isset( $_POST['username'] ) )

或者

if( count( $_POST) > 0)

旁注

不知道为什么要将mysqli程序和OOP风格混合在一起。