开发者

Why isnt this php/ajax working?

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.

Why isnt this php/ajax working?

Why isnt this php/ajax working?

Why isnt this php/ajax working?

Why isnt this php/ajax working?

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

The php was

function getDepartment($keywords){
    $arr = a开发者_运维百科rray();

    $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;
}

Why isnt this php/ajax working?

**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?


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>");
});


Yes i also implemented CONCAT function for LIKE function in where clause but it didn't work.Then i used separate column for like function. Of course, your where clause is not working.

You can implement this code for course radio button function as follows:

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

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

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

    return $arr;
}

Enjoy!!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜