doufen3838 2016-02-10 18:40
浏览 41
已采纳

使用ajax和jquery进行验证

This is what I've done so far.

<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');  
            }  

    });  

But it just shows that username is not proceed.I think that there is a problem with data transfer with my php file.

my validateusername.php file:

<?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
", 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条回答 默认 最新

  • doupai1876 2016-02-10 18:59
    关注

    In your ajax you are only sending username in ajax data:

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

    And in PHP you are using

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

    Your ajax request not contains this. It should be

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

    Or

    if( count( $_POST) > 0)
    

    Side note:

    Don't know why are you mixing mysqli procedural and OOP style together.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能