【问题标题】:Validating with ajax and jquery使用 ajax 和 jquery 进行验证
【发布时间】:2016-05-21 06:50:06
【问题描述】:

这是我到目前为止所做的。

<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();
}   
?>

【问题讨论】:

  • 你没有从 Php 得到任何响应
  • 你将 mysqli 程序和 OOP 风格混合在一起

标签: php jquery sql ajax


【解决方案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; }

【讨论】:

  • 但是我没看出有什么区别兄弟
  • 另外,我注意到使用 JQuery 您需要在此语句中指定包含/ 的路径:$.post("validateusername.php", { username: username },
  • 没有 OP 检查未定义的 button1 或不需要检查我的答案
  • 现在我明白了,但我认为我必须在您的代码中打印 0 和 1,反之亦然。如果文件 url 在同一个文件夹中,我是否必须将根添加到文件 url。?
  • 是的,请参阅@devpro 答案以获取您的问题的解决方案。您的 ajax 正在发送参数用户名,因此您必须在 $_POST 作为 ['username']
【解决方案2】:

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

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

在 PHP 中你正在使用

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

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

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

或者

if( count( $_POST) > 0)

旁注

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

【讨论】:

  • @jigar-solanki 测试一下。
  • 我已经测试了你的代码,它运行良好,但它仍然显示用户名没有继续。我已经打印了结果变量,它可以很好地打印 0 和 1。知道什么是问题。
  • @jigar-solanki 需要调试使用console.log(result);在ajax成功或警报(结果)内;看看你得到了什么
  • 试过它给出的html文件在body标签中包含1。
  • 和 0 为占用的用户名。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-08
  • 2012-04-30
  • 2011-07-11
  • 2011-07-27
相关资源
最近更新 更多