2012-10-16 21 views
2

第一個問題是,我有一個自動完成它的作品找到當我完成鍵入第一個名稱它加載完美,但是當我按空格繼續鍵入姓氏div隱藏並不繼續與搜索結果。我缺少一些在PHP文件或jquery文件。jQuery自動建議使用PHP

這是搜索欄

<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 
} 
} 
?> 

和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"); 
    }); 
+1

是否有一個原因,你沒有使用jQuery UI自動完成插件? – jbabey

回答

0

當你在名字後鍵入空格,它不能返回任何內容,因爲搜索短語按原樣傳遞並在中查找分別爲和lastname列。看看你的查詢:

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"不像"%John %"

您將需要更改where子句。您可以試試:

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

感謝Michal Klouda的提示,但我試過了,但它沒有給出任何結果,div也沒有加載出來既不顯示結果 – user1299444

0

這是哪裏查詢和多感謝米哈爾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");