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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀