使用ajax进行双实时搜索

ru9i0ody  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(266)

此示例如下: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);
    }

当声明get value key cabang时,第一个livesearch“name”就不起作用了。我想我的声明有问题。可能在将index.php中的值传递给search.php时,数据未发送。或者jquery中的错误。任何帮助都将不胜感激。谢谢

kqqjbcuj

kqqjbcuj1#

它工作正常。。。如果你想让第二个文本框工作,你需要更新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
    });

});

相关问题