dqan70724 2012-07-20 10:57
浏览 41
已采纳

在php oop结构中检查Ajax用户名可用性

Understanding how ajax works has been really difficult for me. I think it's mainly because, somehow, I ended up writing php code in oop structure (I am a beginner, and it happened because of this php book about oop I read when I first started learning php), and I seem to have a problem connecting php class file (functions inside it) to ajax function.

I am trying to add a real-time username availability check feature to my web application (like the one you see in twitter's signup page).

I have two files, register.php and class.register.php

Inside register.php file, there are some php code that fires class.register.php file, some html code, and some ajax code (haven't separated javascript file yet).

php part of register.php file looks like this,

<?php
require_once 'class/class.register.php';
$register = new Register();
if(isset($_POST['picked_username'])){
    $register->ajax_username();
}
?>

html part of register.php file looks like this,

<input type="text" name="username_input" id="username_input" value="" />
<span id="validate_username"></span>

ajax part of register.php file looks like this,

$("#username_input").keyup(function(){

    var picked_username = $("#username_input").val();
    $("#validate_username").text("loading…");

    $.ajax({
        type:"post",
        url:"register.php",
        data:{"picked_username":picked_username},
        success:function(result){
            if(result == "0"){
                $("#validate_username").text("available");
            }else if(result == "1"){
                $("#validate_username").text("not available");
            }
        },
        error:function(){
            $("#validate_username").text("something is wrong");
        }
    });
    return false;
});

So basically, when you type something in the username_input area, it sends the data to the same page (register.php), and then triggers isset($_POST['picked_username']) php code on top of the page, which then calls ajax_username(); function in the class.register.php file.

And this is the ajax_username(); function in class.register.php file (used PDO).

public function ajax_username(){
    $picked_username = $_POST['picked_username'];

    $query = "SELECT username FROM mytable WHERE username = '$picked_username' LIMIT 1";
    $result = $this->db->query($query);
    $number = $result->rowCount();

    if($number == 0){
        echo '0';
    }elseif($number == 1){
        echo '1';
    }
}

And unfortunately, nothing happens. Can anyone please tell me what the problem is?

When I do UPDATE or INSERT query instead of SELECT, it actually works fine. For instance, I used a very similar code to update checkbox value in the database with ajax.

public function update_checkbox(){
    $check_box = $_POST['check_box'];
    $query = "UPDATE mytable SET checkbox = '$check_box' WHERE id = $myid LIMIT 1";
    $result = $this->db->query($query);
}

I think this code works because it doesn't echo anything. And I don't think my ajax code is receiving any value from echo.

Please help me! I've been struggling with this problem for a while. Thank you so much in advance!

  • 写回答

2条回答 默认 最新

  • dpw70180 2012-07-20 13:55
    关注

    Ok so thinking about what you said regarding the HTML being returned. Your code is this...

    <?php
    require_once 'class/class.register.php';
    $register = new Register();
    if(isset($_POST['picked_username'])){
        $register->ajax_username();
    }
    ?>
    

    The problem (or at least part) is after $register->ajax_username(); is called I imagine the rest of your HTML page is being called after it. You will need to use exit() to prevent any additional code from being run. Try this

    if($number == 0){
        exit('0');
    }else{
        exit('1');
    }
    

    Also I can see rupesh-patel's point. The above is probably a more elegant solution. (we don't really need to check for any value other than 0).

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

报告相同问题?

悬赏问题

  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型