JSON mysql自动完成表单中的多个输入

时间:2015-04-02 09:44:22

标签: php jquery mysql json pdo

使用json autocomplete进行一个小问题,它应该从数据库中完成多个字段。获得一个结果没有戏剧性,但我的项目需要从mysql数据库填充3-4个字段。

这里是html

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/base/minified/jquery-ui.min.css" type="text/css" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/ui/1.10.1/jquery-ui.min.js"></script> 
<script type="text/javascript">

$(document).ready(function(){
    var ac_config = {
        source: "autocomp.php",
        select: function(event, ui){
            $("#bookname").val(ui.item.bookname);
            $("#book_id").val(ui.item.book_id);
            $("#author").val(ui.item.author);
        },
        minLength:1
    };
    $("#bookname").autocomplete(ac_config);
});
</script>
<form action="#" method="post">
     <p><label for="bookname">Book</label><br />
         <input type="text" name="bookname" id="bookname" value="" /></p>

     <p><label for="book_id">ID</label><br />
         <input type="text" name="book_id" id="book_id" value="" /></p>

     <p><label for="author">Author</label><br />
         <input type="text" name="author" id="author" value="" /></p>
</form>
</body>
</html>

继承人的第一个php

<?php
session_start();
require('../includes/config.php');


$query = "SELECT * FROM books WHERE bookname LIKE ?";
$result = array("%$bookname%");
$stmt = $db->prepare($query);
$stmt->execute($result);

$term = trim(strip_tags($_GET['term']));

$matches = array();
foreach($result as $row){
    if(stripos($row['bookname'], $term) !== false){

        $row['value'] = $row['bookname'];
        $row['label'] = "{$row['bookname']}, {$row['book_id']} {$row['author']}";
        $matches[] = $row;
    }
}

$matches = array_slice($matches, 0, 5);
print json_encode($matches);

和我试过的第二个php

<?php
session_start();
require('../includes/config.php');


        $book_id = 'book_id';
        $author = 'author';
        $bookname = 'bookname';
        $term = 'term';

            $query = $db->prepare("SELECT * FROM books WHERE bookname LIKE :term");
            $query->execute(array(
                ':book_id'=>$book_id,
                ':author'=>$author,
                ':bookname'=>$bookname,
                ':term'=>$term));
            $result = $query -> fetchAll();


$term = trim(strip_tags($_GET['term']));

$matches = array();
foreach($result as $row){
    if(stripos($row['bookname'], $term) !== false){

        $row['value'] = $row['bookname'];
        $row['label'] = "{$row['bookname']}, {$row['book_id']} {$row['author']}";
        $matches[] = $row;
    }
}

$matches = array_slice($matches, 0, 5);
print json_encode($matches);

我已经搞乱了2天而且我无法使它发挥作用。也许聪明的人可以看到问题?

0 个答案:

没有答案