[英]jQuery autosuggest using PHP
first question is that i have an auto complete it works find when i complete type the first name it load perfect but when i press space to continue to type the lastname the div hides and doesn't continued with the search result.i am missing something in the php file or the jquery file. 第一个问题是我有一个自动完成功能,当我完成输入的名字时,它可以找到完美的名字,但是当我按空格键继续输入姓氏时,div会隐藏并且不会继续搜索结果。在php文件或jquery文件中。
this is the search bar 这是搜索栏
<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>
this is the search.php file to query the database 这是用于查询数据库的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> <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
}
}
?>
and the jquery to load the div 和jQuery来加载div
$(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");
});
When you type space after first name, it can't return anything, since the search phrase is passed as is and is looked up in firstname
and lastname
columns separately. 当您在名字后面输入空格时,它不会返回任何内容,因为搜索词组将按原样传递,并分别在firstname
和lastname
列中查找。 Look at your query: 查看您的查询:
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
If you have John Smith in your table, he won't be found, since "John"
is not like "%John %"
. 如果您的表中有John Smith,则不会找到他,因为"John"
与 "%John %"
。
You will need to change the where clause. 您将需要更改where子句。 You might try: 您可以尝试:
where firstname + ' ' + lastname like '%$q%' or
lastname + ' ' + firstname like '%$q%'
it was the where query and much thanks to Michal Klouda for the tip where the problem lies. 这是查询的地方,非常感谢Michal Klouda提供了问题所在的提示。
$sql_res=mysql_query("SELECT user_id, firstname, lastname, email, country, gender
FROM users where lower(concat_ws(' ', firstname, lastname)) like '%$q%' ORDER BY email_activated = '1'
LIMIT 5");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.