使用ajax进行双重实时搜索

时间:2016-10-05 08:37:53

标签: php jquery mysql ajax

以下示例: https://codeforgeek.com/2014/09/ajax-search-box-php-mysql/

我创建一个输入livesearch。但我想要更多的投入。 这就是我要做的事情:

$(document).ready(function(){
    $('input.nama').typeahead({
        name: 'nama',
        remote:'search.php?keynama=%QUERY',
        limit : 10
    });

     $('input.cabang').typeahead({
        name: 'cabang',
        remote:'search.php?keycabang=%QUERY',
        limit : 10
    });

});

这是输入类型:

<input type="text" name="nama" class="nama livesearch tt-query" autocomplete="off" spellcheck="false" placeholder="Nama">
<input type="text" name="cabang" class="cabang livesearch tt-query" autocomplete="off" spellcheck="false" placeholder="Cabang">

search.php中的最后一个代码

$keynama=$_GET['keynama'];
    $nama = array();

    //$keycabang=$_GET['keycabang'];
    //$cabang = array();

    $con=mysql_connect("localhost","root","");
    $db=mysql_select_db("dbdataorder",$con);

    if($keynama!=null)
    {
        $query=mysql_query("SELECT  idNoon,notaris,noon,cabang,nama,proses,tanggalOn,tanggalCair,keterangan 
        FROM tblNotaris,tblOrderNotaris
        where tblNotaris.idnotaris=tblOrderNotaris.idnotaris and nama LIKE '%{$keynama}%'");

        while($row=mysql_fetch_assoc($query))
        {$nama[] = $row['nama'];}
        echo json_encode($nama);
    }

当声明获取值键值时,第一个livesearch&#34; name&#34;变得不起作用。 我认为我的宣言有问题。也许当从index.php传递值到search.php数据时不发送。或jquery中的错误。任何帮助,将不胜感激。感谢

1 个答案:

答案 0 :(得分:1)

它工作正常...如果你想让第二个文本框工作,你需要更新你的search.php ....

$keynama=$_GET['keynama'];
$nama = array();

$keycabang=$_GET['keycabang'];
$cabang = array();

$con=mysql_connect("localhost","root","root");
$db=mysql_select_db("dbdataorder",$con);

if($keynama!=null)
{
    $query=mysql_query("SELECT  idNoon,notaris,noon,cabang,nama,proses,tanggalOn,tanggalCair,keterangan 
    FROM tblNotaris,tblOrderNotaris
    where tblNotaris.idnotaris=tblOrderNotaris.idnotaris and nama LIKE '%{$keynama}%'");

    while($row=mysql_fetch_assoc($query))
    {$nama[] = $row['nama'];}
    echo json_encode($nama);
}


if($keycabang!=null)
{
    $query=mysql_query("SELECT  idNoon,notaris,noon,cabang,nama,proses,tanggalOn,tanggalCair,keterangan 
    FROM tblNotaris,tblOrderNotaris
    where tblNotaris.idnotaris=tblOrderNotaris.idnotaris and cabang LIKE '%{$keycabang}%'");

    while($row=mysql_fetch_assoc($query))
    {$cabang[] = $row['cabang'];}
    echo json_encode($cabang);
}

下面给出了JS代码......

$(document).ready(function(){

    $('input.nama').typeahead({
        name: 'nama',
        remote:'search.php?keynama=%QUERY',
        limit : 10
    });

     $('input.cabang').typeahead({
        name: 'cabang',
        remote:'search.php?keycabang=%QUERY',
        limit : 10
    });

});