weixin_33739523 2017-11-09 22:07 采纳率: 0%
浏览 23

AJAX / PHP搜索不起作用

I am attempting to create a searchbox using php and AJAX. However, when I type a name already in the database into searchbox, no suggestion appear. Nothing happens.

I have tested the php and seems to be working. What am I missing?

Code update with PDO

require_once('connect_pdo.php');

$searchTerm = $_GET['term'];

$stmtus = $conn->prepare("SELECT * FROM `Schools` WHERE `School` LIKE :School");
$stmtus->bindValue(':School', '%' . $searchTerm . '%');
$searchTerm = $_POST['school'];;
$stmtus -> execute();


while ($result = $stmtus->fetch(PDO::FETCH_ASSOC)) {

$school = $result['School'];

}
echo json_encode($school);

AJAX

 <script type="text/javascript"
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript"
    src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
    <link rel="stylesheet" type="text/css"
    href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" />

    <script type="text/javascript">
            $(document).ready(function(){
                $("#school").autocomplete({
                    source:'schoolfind.php',
                    minLength:3
                });
            });
    </script>

HTML

<label>What school does the child attend<input  type="text" name="school" id="school" /></label>
  • 写回答

1条回答 默认 最新

  • weixin_33725515 2017-11-09 22:10
    关注

    Any bind anchor you use (like :School) needs to appear unquoted in your query string. I.e., don't do this:

    $stmtus = $conn->prepare("SELECT * FROM `Schools` WHERE `School` LIKE '%".$searchTerm."%'");
    $stmtus->bindParam(':School', $searchTerm);
    

    Do this:

    $stmtus = $conn->prepare("SELECT * FROM `Schools` WHERE `School` LIKE :School");
    $stmtus->bindValue(':School', '%' . $searchTerm . '%');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了