ajax live search,javascript - AJAX Live Search Not Showing Results - Stack Overflow

Have a live search page as shown in livesearch.php below, when typing entries into the input box am not getting any results show under it. The html code and script in search.php are shown beelow.

search.php

Search Schedules

Enter any information you wish, this will search all schedule fields.

$(document).ready(function() {

$('#keyword').on('input', function() {

var searchKeyword = $(this).val();

if (searchKeyword.length >= 3) {

$.post('livesearch.php', { keywords: searchKeyword }, function(data) {

$('ul#liveSearch').empty()

$.each(data, function() {

$('ul#liveSearch').append('

' + this.title + '');

});

}, "json");

}

});

});

The livesearch.php that is being referenced by the script is shown below

livesearch.php

session_start();

require 'dbconnect.php';

echo "Keyword: ".$_POST['keywords']."
";

$liveSearchArray = array();

if (!empty($_POST['keywords'])) {

$keywords = $connection->real_escape_string($_POST['keywords']);

$sql = "SELECT OWNER_SURNAME,OWNER_FIRSTNAMES,OWNER_TITLE FROM testucm_owners WHERE OWNER_SURNAME LIKE '%".$keywords."%'";

$result = $connection->query($sql);

if ($result->num_rows > 0) {

echo "We Have A Result, There Are ".$result->num_rows." Rows";

while ($obj = $result->fetch_object()) {

$liveSearchArray[] = array('id' => $obj->OWNER_SURNAME, 'title' => $obj->OWNER_TITLE);

}

} else {

echo "No Matches";

}

}

echo json_encode($liveSearchArray);

mysqli_close($connection);

?>

If a manually add a value for keywords into the livesearch.php query I get the correct results, however no resutls display if I enter search terms via search.php. I have partially test this by putting an alert after var searchKeyword = $(this).val();, this shows the correct term as typed in however still no results showing.

I suspect the error may be with this line of code:

$('ul#liveSearch').append('

' + this.title + '');

Either that or for some reason the $liveSearchArray is not being passed back to the script, however I'm unable to determine where the error lies or how to fix it. Any help would be greatly appreciated

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值