doubai9014 2019-05-28 14:54
浏览 45

实时搜索picture_name

The names of picture from database do not appear when I'm typing its letters in the input of my form. I'm trying to implement the live search on my page. I got MySql database called 'pinterest' with table 'pictures' and 'picture_name' column with different names of picture. I want these names to appear when I'm typing letters of it in the input of my form. NOTHING WORKS :-(

I got MySql Database, php form and ajax request.

file : search.php:

require_once __DIR__.'/connect.php';
?>

<form id="frmSearch" method="post" action="search.php">
<label>Pickup location <br>
      <input type="text" name="txtSearchPictureName" id="txtSearchPictureName" placeholder="Search.." list="searchList" required>
      <datalist id="searchList"></datalist>
    </label>
</form>

<script src="search.js"></script>

file : search.js

$('#txtSearchPictureName').on('input', function(val){
    $.ajax({
      method : "GET",
      url : 'apis/api-search-picture.php?txtSearchPictureName='+val.target.value,
      cache: false,
      dataType:"JSON"
    }).
    done( function(jData){
        $('#searchList').empty()
        jData.forEach(x => {
            $('#searchList').append('<option value="' + x.picture_name + '"></option>')
        })
    }).
    fail( function(){

    })
  })

file: api-search-picture.php

<?php

 //connection do db
 require_once __DIR__.'/connect.php';

$sPictureName = $_GET['txtSearchPictureName'];


try{
    $stmt = $db->prepare('SELECT picture_name FROM pictures WHERE picture_name LIKE :sPictureName ');
    $stmt->bindValue(':sPictureName', "%$sPictureName%"); 
    $stmt->execute();
    $aRows = $stmt->fetchAll();


    echo json_encode($aRows);


} catch(PDOEXception $ex){
    echo $ex;
}
```
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
    • ¥20 关于URL获取的参数,无法执行二选一查询
    • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
    • ¥15 marlin编译错误,如何解决?
    • ¥15 有偿四位数,节约算法和扫描算法
    • ¥15 VUE项目怎么运行,系统打不开
    • ¥50 pointpillars等目标检测算法怎么融合注意力机制
    • ¥20 Vs code Mac系统 PHP Debug调试环境配置
    • ¥60 大一项目课,微信小程序
    • ¥15 求视频摘要youtube和ovp数据集