duanguane1670 2019-04-29 04:14
浏览 91

使用PHP和MYSQL通过jquery ui autocomplete搜索栏

I want to add a search bar in my website, so I added jQUery UI autocomplete search and it works like a charm and the results are showing from the database. But, I need if the user clicks on a result from the list I want to redirect the user to that product page. This is the code in navigation.php:

<div id="search-div">
  <form action="#" id="form-search" method="POST" class="form-inline">
    <input id="language" type="text" placeholder="Search for a product" style="padding-left:15px;">
    </form>
</div>

<script>
$("#language").autocomplete({
  appendTo: $("#language").parent(),
  source: function(data, cb){
    $.ajax({
      url: '/tutorial/admin/parsers/ajax.php',
      method: 'GET',
      dataType: 'json',
      data: {
        title: data.term
      },
      success: function(res){
      cb(res);
      }
    });
  }
});
</script>

And this is ajax.php code:

<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/tutorial/core/init.php';
$data = array();
if (!empty($_GET['title'])) {
  $title = strtolower(trim($_GET['title']));
  $sql = "SELECT title FROM products WHERE title LIKE '" .$title. "%'";
  $result = $db->query($sql);
  while ($row = mysqli_fetch_assoc($result)) {
    array_push($data, $row['title']);
  }
}
echo json_encode($data);
exit;
 ?>

The page that I want the user to be redirected to is products.php?details=20 where 20 is the id of the product it is a GET method page. How to make the ability to redirect the product page based on the id if the user clicks on this product from the search list?

  • 写回答

3条回答 默认 最新

  • douyun1546 2019-04-29 04:27
    关注

    Try this:

      $(document).ready(function() {
        $("#language").autocomplete({
            source: function(data, cb){
        $.ajax({
          url: '/tutorial/admin/parsers/ajax.php',
          method: 'GET',
          dataType: 'json',
          data: {
            title: data.term
          },
          success: function(res){
          cb(res);
          },
            select: function( event, ui ) {
                window.location.href = ui.item.link;
            }
        });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)