weixin_33738555 2011-03-31 01:03 采纳率: 0%
浏览 23

为什么这个php / ajax不起作用?

Basically, I have a live search that is working for the two out of 3 radio buttons. The "Professor", and the "Department" both work fine. But the "Course" radio code doesn't seem to be working. I don't get it. Professor

Department

Before I type anything in it gives me this

If I type in "Eng" for "Engineering" or "English", I get lesser options, but still undefined

It seems the Ajax is working, but for some reason it isn't pulling these fields.

The php was

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

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

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

    return $arr;
}

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

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

    while( $row = mysql_fetch_array ( $query ) )
    {
        $arr[] = array( "id" => $row["cID"], "course" => $row["prefix"] . ' ' . $row["code"]);
    }

    return $arr;
}

The columns in phpmyadmin

**Javascript

<!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';
                    ext = '.php?pID=';
                    break;
                case 'department': 
                    phppage = 'department';
                    ext = '.php?dID=';
                    break;
                case 'course': 
                    phppage = 'course';
                    ext = '.php?cID=';
                    break;
            } 

            $.each(data, function(){
                $("div#content").append("- <a href='" + phppage + ext + 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" style="background-color:#eee;"></div>

</body>
</html>

The one column "code" is a numerical value such as 153 or both "Prefix and "Code" together would be something like "INFO 153".

Anyone? Does Ajax have a limit on the number of records it can pull? This course table maybe has 1200 courses, but why are they undefined?

  • 写回答

2条回答 默认 最新

  • weixin_33695450 2011-03-31 01:29
    关注

    You're retrieving the name, but when searching for a course, the name has been renamed to course. You can fix this in the PHP by changing this line:

    $arr[] = array( "id" => $row["cID"], "course" => $row["prefix"] . ' ' . $row["code"]);
    

    To this:

    $arr[] = array( "id" => $row["cID"], "name" => $row["prefix"] . ' ' . $row["code"]);
    

    Also, unrelatedly, in the JavaScript, you are setting ext without declaring it. You should probably change this line:

    var phppage;
    

    To this:

    var phppage, ext;
    

    Edit: This is how you would style the items as a proper bulleted list:

    var content=$("#content");
    var ul=$("<ul>");
    content.append(ul);
    $.each(data, function(){
        ul.append('<li><a href="' + phppage + ext + this.id + '">' + this.name + "</a></li>");
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决