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条)

报告相同问题?

悬赏问题

  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致