dqbn76906 2014-05-07 10:37
浏览 96
已采纳

AJAX搜索和JSON响应

I'm trying to do some simple AJAX searching with jQuery and PHP. However I can't seem to get the proper search string to work. I want to search by title, and if the title hasn't seen set, just display all results when clicking search. Also I would like to show the results as beautiful HTML and not returned back in JSON-like-code. Somthing like:

    $book["title"]
    $book["author"]
    $book["description"]

SQL Set-up:

Table Name: books Table Fields: id, title, author, description

HTML:

    <div id="search">
        <form action="#">
            <p><label for="title">Book Title:</label> <input type="text" id="search_title" name="search_title"></p>
            <p><input type="submit" id="search_submit" name="search_submit" value="Search!"></p>
            <p><em><small>For example A Game of Thrones or The Lord of the Rings</small></em></p>
            <hr>
        </form>
    </div>
    <div id="search_results">

    </div>
    <script>
        $(document).ready(function() {
            $("#search_submit").on("click", function() {
                var searchTitle = $("#search_title").val(),
                    data = 'title=' + searchTitle;

                if(searchTitle) {
                    $.ajax({
                        type: "POST",
                        url: "getBooks.php",
                        data: data,
                        success: function(res)
                        {
                            $("#search_results").html(res);
                        }
                    });    
                }
                return false;
            });
        });
    </script>

PHP:

if(isset($_GET["title"])) {
    $title = $_GET["title"];
}

if(isset($title) && !empty($title)) {
    $pdo_title = "WHERE title LIKE '%" . $title . "%'";
} else {
    $pdo_title = "";
}

$pdo_books = "books";

$pdo = new PDO("mysql:dbname=removed;host=removed","removed","removed");
$statement = $pdo->prepare("SELECT * FROM $pdo_books $pdo_title");
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);
echo $json;
  • 写回答

3条回答 默认 最新

  • duanduoding2238 2014-05-07 10:44
    关注

    If its about formatting output in HTML from JSON, Then in your jQuery code you would need to parse json and repeat loop for each json objects in an array and add HTML to your page dynamically.

    if(searchTitle) {
        $.ajax({
            type: "POST",
            url: "getBooks.php",
            data: data,
            success: function(res)
            {
                var my_table="<table>";
                $.each(res, function(i, obj){
                    my_table+="<tr> <td> "+obj.clumn_name+" </td> <td> "+obj.clumn_name2+" </td> </tr> ";
                });
                my_table+"</table>";
                $("#search_results").html(my_table);
            }
        }); 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dqkyz02602 2014-05-07 10:41
    关注

    You have mentioned the method as post in ajax and you are trying with $title = $_GET["title"];

    Try using

    $title = $_POST["title"];
    
    评论
  • dongqiong8021 2014-05-07 10:42
    关注
    if(isset($_POST["title"])) {
      $title = $_POST["title"];
    }
    
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 端口转发器解析失败不知道电脑设置了啥
  • ¥15 Latex算法流程图行号自定义
  • ¥15 关于#python#的问题:我在自己的电脑上运行起来总是报错,希望能给我一个详细的教程,(开发工具-github)
  • ¥40 基于51单片机实现球赛计分器功能
  • ¥15 cs2游戏画面卡住,应用程序sid与指挥者sid不匹配
  • ¥15 实验七:Pandas要有实验截图和代码
  • ¥15 TypeError: Make sure that the iterable only contains strings.
  • ¥35 电脑放图书馆,这是被黑了吗
  • ¥15 等高线中数据取消科学计数法
  • ¥15 Qt播放10路ffmpeg 视频流