weixin_33720956 2015-11-30 20:11 采纳率: 0%
浏览 12

带有类别搜索的ajax php

Hello I am developing an ajax php search. It will search through the database accross multiple tables when someone types. My question is that i need it to bring up a name like if someone types the name Apple tv i want it to bring up do you want the category or the specific product?. How can i do that?

This is what I have so far:

<?php
//Database values
$host = "localhost";
$dbname = "tool";
$user = "root";
$password = "root";
$database = new mysqli();

$database->connect($host, $user, $password, $dbname);

//Kill website if database connection fails
if($database->connect_errno) {
die("Database connection failed.");
}

//Connect to the database
//Clean user input
$search = $database->real_escape_string($_POST['search']);
$search = preg_replace("/[^A-Za-z0-9 ]/", '', $search);
$search = "'%".$_POST['search']."%'";

$query = "SELECT * FROM brands WHERE braname LIKE $search ORDER by braname ASC LIMIT 5";

if($results = $database->query($query)){
while ($player = $results->fetch_assoc()){
echo "<div class='col-sm-4' id='adjust-Searchbox'>" . "<a href=\"users/e-commerceTemplateSingleBrand.php?braid=".$player['braid']."\">" . "<p>" . $player["braname"] . "</p>" . "<img style='width:100%; height:50px;' src=\" uploads/".$player['braimg']."\">" . "</a>" . "</div>"

;
}
}else{
die("Database connection failed.");
}

$search = $database->real_escape_string($_POST['search']);
$search = preg_replace("/[^A-Za-z0-9 ]/", '', $search);
$search = "'%".$_POST['search']."%'";

$query = "SELECT * FROM Products WHERE name LIKE $search ORDER by name ASC LIMIT 5";

if($results = $database->query($query)){
while ($player = $results->fetch_assoc()){
echo "<div class='col-sm-4' id='adjust-Searchbox'>" . "<p>" . $player["name"] . "</p>" . "<img style='width:100%; height:50px;' src=\" uploads/".$player['proimg']."\">" . "</br>" . "</div>";
}
}else{
die("Database connection failed.");
}
  • 写回答

1条回答 默认 最新

  • weixin_33725722 2015-11-30 20:26
    关注

    Create the search text field

    <input onkeyup="do_search();" type="text" value="" id="search_string">
    

    Then add your javascript to call the ajax

    function do_search() {
    $('#the_div_to_show_ajax_return').html('Please wait ...');
    $.ajax({
    type: "POST",
    url: "yourcode.php",
    data: {search: $('#search_string').val()}
    })
    .done(function( msg ) {
        $('#the_div_to_show_ajax_return').html(msg);
    });
    }
    

    Also make sure you have a div ready for the results

    <div id="the_div_to_show_ajax_return"></div>
    
    评论

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上