gpt4 book ai didi

php - 使用 PHP 的 jQuery 自动建议

转载 作者:行者123 更新时间:2023-11-30 06:38:50 25 4
gpt4 key购买 nike

第一个问题是我有一个自动完成功能,当我完成输入名字时它可以正常加载,但是当我按空格键继续输入姓氏时,div 会隐藏并且不会继续搜索结果。我我在 php 文件或 jquery 文件中遗漏了一些东西。

这是搜索栏

<form method='post' action='index2.php#profile_info.php'>
<input type="text" id='inputSearch' placeholder="Search" autocomplete="off" class="search" />
<input type='submit' id='Search_Submit' value='' />
</form>
<div id="divResult"</div>

这是查询数据库的search.php文件

<?php
include('connect.php');
if($_POST)
{
$q=$_POST['searchword'];
$sql_res=mysql_query("SELECT user_id, firstname, lastname, email, country, gender from users where firstname like '%$q%' OR lastname like '%$q%' order by email_activated='1' LIMIT 5");
while($row=mysql_fetch_array($sql_res))
{

$id = $row['user_id'];
$firstname=$row['firstname'];
$lastname=$row['lastname'];
$email=$row['email'];
$country=$row['country'];
$Gender=$row['gender'];
$cacheBuster = rand(999999999,9999999999999);

$check_pic = "members/$id/image01.jpg";
$default_pic = "members/image01.jpg";
if (file_exists($check_pic)) {
$users_pic ="$check_pic?$cacheBuster"; // forces picture to be 100px wide and no more
}else {
$users_pic = "images/profiles/".$Gender."_small.jpg"; // forces default picture to be 100px wide and no more

}

$b_firstname='<b>' .$q. '</b>';
$b_lastname='<b>'.$q.'</b>';
$final_firstname = str_ireplace( $q, $b_firstname, $firstname);
$final_lastname = str_ireplace( $q, $b_lastname, $lastname);
?>
<div class="display_box" align="left" >
<a href="http://localhost/MyNewSite/index2.php?user_id='<?php echo $id; ?>'#status.php" target="_self"> <img src="<?php echo $users_pic; ?>" style="width:55px; height:50px; float:left; margin-right:6px;" /></a><span class="name"><a href="http://localhost/MyNewSite/index2.php?user_id='<?php echo $id; ?>'#status.php" target="_self" class="friendsLink"><?php echo " $final_firstname $final_lastname"; ?> </a></span>&nbsp;<br/><span style="font-size:10px; margin-left:6px;"><?php echo $email; ?></span><br/>
<span style="font-size:10px; color:#C40000; margin-left:6px;"><?php echo $country; ?></span></div>

<?php
}
}
?>

和加载div的jquery

$(function(){
$(".search").keyup(function()
{
var inputSearch = $(this).val();
var dataString = 'searchword='+ inputSearch;
if(inputSearch!='')
{
$.ajax({
type: "POST",
url: "search.php",
data: dataString,
cache: false,
success: function(html)
{
$("#divResult").html(html).show();
}
});
}return false;
});

jQuery("#divResult").live("click",function(e){
var $clicked = $(e.target);
var $name = $clicked.find('.name').html();
var decoded = $("<div/>").html($name).text();
});
jQuery(document).live("click", function(e) {
var $clicked = $(e.target);
if (! $clicked.hasClass("search")){
jQuery("#divResult").fadeOut();
}
});
$('#inputSearch').click(function(){
jQuery("#divResult").fadeIn();
});
});


jQuery(function($){
$("#inputSearch").Watermark("Search");
});

最佳答案

当您在名字后键入空格时,它不会返回任何内容,因为搜索短语按原样传递并分别在 firstnamelastname 列中查找.查看您的查询:

SELECT user_id, firstname, lastname, email, country, gender 
from users
where firstname like '%$q%' OR lastname like '%$q%' /* <- here's the problem */
order by email_activated='1' LIMIT 5

如果您的表中有 John Smith,将找不到他,因为"John" 不像 "%John %".

您需要更改 where 子句。您可以尝试:

where firstname + ' ' + lastname like '%$q%' or
lastname + ' ' + firstname like '%$q%'

关于php - 使用 PHP 的 jQuery 自动建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12916592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com