0

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!

4

2 回答 2

1

好的,考虑一下您所说的有关返回的 HTML 的内容。你的代码是这样的...

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

问题(或至少部分)是在$register->ajax_username();被调用之后我想你的 HTML 页面的其余部分是在它之后被调用的。您将需要使用exit()来防止运行任何其他代码。尝试这个

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

我也可以看到 rupesh-patel 的观点。以上可能是一个更优雅的解决方案。(我们真的不需要检查 0 以外的任何值)。

于 2012-07-20T13:55:11.750 回答
1

通过查看您的代码,我可以在 ajax_username() 函数中指出一件事

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

如果您的号码既不是 0 也不是 1 会发生什么,我认为 $number 是 NULL 因为$result->rowCount()返回 NULL。你没有包括这个案例。改用这个

if($number == 0){
        echo '0';
    }elseif($number == 1){
        echo '1';
    }
    else
    {
       echo '0'; //or any thing else
    }
于 2012-07-20T11:32:25.300 回答