0

私は asp.net と jquery を勉強しています。現在、PHP で単純なオートコンプリートを実装しようとしています。次のようなことができました。

クライアントコードは次のとおりです。

<link type="text/css" rel="stylesheet" href="css/jquery-ui-1.8.17.custom.css"/>
    <script type="text/javascript" src="jquery-1.7.1.js"></script>
    <script type="text/javascript" src="jquery-ui-1.8.5.custom.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function()
        {
            $('.auto').autocomplete(
            {
                source: "search.php", 


                focus: function(event, ui) {
        $(idField).val(ui.item.value);
        $(this).val(ui.item.label);
        return false;
    },

                select: function(event, ui) {
        //$(this).val(ui.item.label);
        $(this).val(ui.item.label);
        var a = "#"+$(this).attr('id');

        $(a+"hidden").val(ui.item.value);
        return false;
    }
                //minLength: 3
            });
        });
    </script>

検索を行うコードは次のとおりです。

<?php

$host = "localhost"; 
$user = "root"; 
$password = ""; 
$db = "isproj2"; 

// open connection 
$connection = mysql_connect($host, $user, $password) or die ("Unable to connect!"); 

// select database 
mysql_select_db($db) or die ("Unable to select database!"); 
$text = mysql_real_escape_string($_GET['term']);


$query = "Select SupplierName, SupplierID from tbl_supplier where SupplierName LIKE '%$text%'";
$result = mysql_query($query);
$data = array();

$first = true;
while ($row = mysql_fetch_array($result)) {
    $data[] = array('label' => $row['SupplierName'], 'value' => $row['SupplierID']);
}
echo json_encode($data);
?>

私がしたいことは、このasp.netのようなことをすることです。いくつかの記事で、Webサービスを使用する必要があると読んだことがあります。

4

1 に答える 1

0

jQueryオートコンプリートには、可能な値を入力するために使用するjsonオブジェクトのみが必要です。サーバー上でどのように生成されるかは関係ありません。あなたは確かに正式なウェブサービスを使用することができます、そして私はasp.netがそれらを生成するための非常にきちんとした方法を持っていると思います。私は専門家ではありません。

asp.netコードから返される応答がphpコードの応答と類似している限り、オートコンプリートは機能します。

于 2012-04-08T13:47:04.553 に答える