为什么这个ajax实时搜索不起作用

时间:2011-03-24 01:46:23

标签: php jquery

* *更新时间代码为3-28

的index.php

   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>jQuery Search Demonstration</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
    <script type="text/javascript">
         $(document).ready(function(){
            $(".keywords").keyup(function(){
                $('#key').html($(this).val());
                $('#table').html($('.table:checked').val());

                // Do the ajax call
                // Get the textbox value: $(this).val()
                // Get the radio value: $('.table:checked').val()

                /*$.post("search.php", { keywords: $(".keywords").val() }, function(data){
                    $("div#content").empty()
                    $.each(data, function(){
                        $("div#content").append("- <a href='prof.php?pID=" + this.pID + "'>" + this.last + "</a>");
                    });
                }, "json");*/
            });
        });
    </script>
    </head>
    <body>
    Search by:
    <input type="radio" name="table" class="table" value="Table 1" />Professor<br />
    <input type="radio" name="table" class="table" value="Table 2" />Department<br />
    <input type="radio" name="table" class="table" value="Table 3" />Course<br />

    <input type="text" name="search" class="keywords">
    <input type="submit" name="submit" class="search">
    <div id="content">
    </div>
    </body>
    </html>

的search.php

<?php
$link = mysql_connect('##',"##","##");
mysql_select_db("###", $link);

$keywords = mysql_real_escape_string( $_POST["keywords"] );

$query = mysql_query("SELECT pID, lname, fname 
                                    FROM Professor 
                                    WHERE CONCAT(lname,fname) LIKE '%". $keywords . "%'");

$arr = array();
while( $row = mysql_fetch_array ( $query ) )
{
    $arr[] = array( "pID" => $row["pID"], "last" => $row["lname"], "first" => $row["fname "] );
}

echo json_encode( $arr );
?>
每个选择的

Sql: [教授]

SELECT pID, lname, fname 
                                    FROM Professor 
                                    WHERE CONCAT(lname,fname) LIKE '%". $keywords . "%'";

[部门]

SELECT prefix, code 
                                    FROM Department 
                                    WHERE name LIKE '%". $keywords . "%'";

[当然]

SELECT前缀,代码                                     从课程                                     WHERE CONCAT(前缀,课程)LIKE'%“。$ keywords。”%'“;

2 个答案:

答案 0 :(得分:1)

您应该像这样更改您的查询:

$query = mysql_query("SELECT pID, lname, fname 
                                    FROM Professor 
                                    WHERE CONCAT(lname,fname) LIKE '%". $keywords . "%'");

请确保json和javascript中的这些字段相同:

$arr[] = array( "id" => $row["pID"], "last" => $row["lname"], "first" => $row["fname"] );

$("div#content").append("- <a href='post.php?id=" + this.id + "'>" + this.first + " " + this.last + "</a>");

HTML code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Search Demonstration</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $(".keywords").keyup(function(){
        getData();
    });
    $(".table").click(function(){
        getData();
    });
});

function getData(){
    $.post("search.php", 
        {
            keywords: $(".keywords").val(),
            table: $('.table:checked').val()
        }, 
        function(data){
            $("div#content").empty();
            var phppage;
            switch($('.table:checked').val())
            {
                case 'professor': 
                    phppage = 'prof';
                    break;
                case 'department': 
                    phppage = 'department';
                    break;
                case 'course': 
                    phppage = 'course';
                    break;
            } 

            $.each(data, function(){
                $("div#content").append("- <a href='" + phppage + ".php?pID=" + this.id + "'>" + this.name + "</a>");
            });
        },
        "json");
}
</script>
</head>
<body>
Search by:
<input type="text" name="search" class="keywords" /><br />

<input type="radio" name="table" class="table" value="professor" checked="checked" /> Professor<br />
<input type="radio" name="table" class="table" value="department" /> Department<br />
<input type="radio" name="table" class="table" value="course" /> Course<br />

<div id="content"></div>

</body>
</html>

PHP代码:

<?php
$link = mysql_connect('localhost',"#","#");
mysql_select_db("#", $link);

$arr = array();
$keywords = mysql_real_escape_string( $_POST["keywords"] );
switch ($_POST["table"])
{
    case 'professor';
        $arr = getProfessor($keywords);
        break;
    case 'department';
        $arr = getDepartment($keywords);
        break;
    case 'course';
        $arr = getCourse($keywords);
        break;
}

echo json_encode( $arr );

function getProfessor($keywords){
    $arr = array();

    $query = mysql_query("SELECT pID, lname, fname 
                            FROM Professor 
                            WHERE CONCAT(lname,fname) LIKE '%". $keywords . "%'");

    while( $row = mysql_fetch_array ( $query ) )
    {
        $arr[] = array( "id" => $row["pID"], "name" => $row["fname"] . ' ' . $row["lname"]);
    }

    return $arr;
}

function getDepartment($keywords){
    $arr = array();

    $query = mysql_query("SELECT prefix, code 
                            FROM Department 
                            WHERE name LIKE '%". $keywords . "%'");

    while( $row = mysql_fetch_array ( $query ) )
    {
        $arr[] = array( "id" => $row["code"], "name" =>  $row["code"]);
    }

    return $arr;
}

function getCourse($keywords){
    $arr = array();

    $query = mysql_query("SELECT prefix, code 
                            FROM Course 
                            WHERE CONCAT(prefix,course) LIKE '%". $keywords . "%'");

    while( $row = mysql_fetch_array ( $query ) )
    {
        $arr[] = array( "id" => $row["code"], "name" => $row["code"]);
    }

    return $arr;
}
?>

答案 1 :(得分:0)

在您的javascript中,您似乎正在尝试访问返回的json对象的.id和.title属性,但它们在您在php中创建的数组中不存在

$arr[] = array( "id" => $row["pID"], "title" => $row["lname"]." ".$row["fname"] );

这有用吗?

编辑:

$(document).ready(function(){
    $(".search").click(function(){
        $.post("search.php", { keywords: $(".keywords").val() }, function(data){
            $("div#content").empty()
            $.each(data, function(){
                $("div#content").append("- <a href='post.php?id=" + this.id + "'>" + this.first + " " + this.last + "</a>");
    });
});

看起来缺少一个括号来封闭$ .each

$(document).ready(function(){
    $(".search").click(function(){
        $.post("search.php", { keywords: $(".keywords").val() }, function(data){
            $("div#content").empty()
            $.each(data, function(){
                $("div#content").append("- <a href='post.php?id=" + this.id + "'>" + this.first + " " + this.last + "</a>");
            });
    });
});