首页 > 解决方案 > 我不能使用 ajax 在我的数据中使用引导实时搜索

问题描述

我正在将 ajax 中的数据导入表中,但是

<select class = "selectpicker" data-live-search = "true">

即使我添加了我的ajax代码也不起作用

 $.ajax({
          url:'/sbadmin/pages/server/get_brand.php',
          type:'GET',
          success:function(result){
            $('#data').html(result);
          }
        });

到餐桌

<select class="selectpicker form-control form-control-xs" onChange="window.location.href=this.value" id="data" data-live-search="true">
    </select>

get_brand.php

$stmt = $db->prepare('SELECT * FROM brands WHERE brand_id>1 ORDER BY brand_name asc');
$stmt->execute();
$brands = $stmt->fetchAll();
?> <option><-- Choose Brand --></option> <?php
foreach ($brands as $brand)
{
    ?>

    <option data-tokens="<?php echo $user['brand_name'] ?>" value="<?php $_SERVER['DOCUMENT_ROOT'] ?>/sbadmin/pages/order/pharmcy/add-pharmacy-order.php?brand_id=<?php echo $brand['brand_id'] ?>"><a href=""><?php echo $brand['brand_name'] ?></a></option>
    <?php
}

标签: phpajaxsearchlive

解决方案


由于您没有在 AJAX 中传递任何值,请尝试如下。我没有测试,但它会工作

            <?php
            function dropValues() { 
          #put connection establishment for DB or declare it globally
            $stmt = $db->prepare('SELECT * FROM brands WHERE brand_id>1 ORDER BY brand_name asc');
            $stmt->execute();
            $brands = $stmt->fetchAll();
            $txt = "";
            foreach ($brands as $brand)
            {
           #edit with your option values. I'm just giving the example.
              $txt .= '<option data-tokens="'.$user['brand_name'].'" value="'.$_SERVER['DOCUMENT_ROOT'].'">$some value</option> ';

            }
            return $txt;
            }


            ?>

            <select class="selectpicker form-control form-control-xs" onChange="someFunction(this)" id="data" data-live-search="true">
            <?php echo dropValues();?>
                </select>
    <script>
    function someFunction(this) { 
    var val = this.val();

    //ajax function here to get the data
    //send the val to php page and get result and append it to drop down



    }

</script>

您的 ajax php 页面完全没有语法。请访问此处了解有关 ajax 的更多信息https://api.jquery.com/jquery.ajax/


推荐阅读