gpt4 book ai didi

php - CodeIgniter事件记录和AJAX返回所有结果并忽略关键字

转载 作者:行者123 更新时间:2023-11-29 23:30:57 25 4
gpt4 key购买 nike

我正在尝试为我的网络应用程序实现“实时搜索”功能。基本思想非常简单:用户输入关键字,AJAX 将处理该请求并返回数据库中与该关键字匹配的所有行。但是,每次我在搜索表单中输入某些内容时,它都会返回所有结果并完全绕过实际关键字(如部分)。

我已经尝试让它工作几个小时了,但无法真正找出问题所在。我尝试过使用和不使用事件记录,它总是为我提供数据库中的所有结果。

这些是它只会考虑的 SQL 语句:

$this->db->where('event_date >=', date('Y-m-d G:i:s'));
$this->db->where('visibility', 1);

这是我的观点:

<!-- Search form -->
<?php $attributes = ['id' => 'search', 'autocomplete' => 'off'];
echo form_open('ajax/search', $attributes);?>
<input type="text" id="app-search" name="keyword" class="search-field" placeholder="Hae tapahtumaa nimellä tai paikkakunnalla...">
<i class="fa fa-search search-icon"></i>
<input type="submit" name="search" class="search-button" value="Hae">
<?php echo form_close();?>

<!-- div for the results -->
<div class="ajax-search-results">
<div class="spinner-for-search" id="search-list">
<i class="fa fa-circle-o-notch fa-2x fa-spin" id="ajax-loading-search" style="text-align: center;"></i>
</div>
</div>

这是我的 JS(用于 AJAX 请求):

$(".ajax-search-results").hide();
$("#app-search").on("keyup", function(e) {
if($("#app-search").val().length > 5) {

$(".ajax-search-results").show();
$("#ajax-loading-search").show();

$.ajax({
type: "post",
url: $("#app-search").parent('form').attr('action'),
cache: false,
data:'search='+$("#app-search").val(),
success: function(response) {
$('.ajax-search-results').html("");
var obj = JSON.parse(response);
if(obj.length > 0){
try{
var items=[];
$.each(obj, function(i,val){
items.push($('<li/><a href=' + window.location.origin + '/Asiakastyot/STCodeIgniter/event/'+ val.event_id +'/>').text(val.event_name + "-" + val.event_city));
});
$('.ajax-search-results').append.apply($('.ajax-search-results'), items);
}catch(e) {
alert('Tapahtui virhe, yritä uudelleen tai ota yhteyttä palveluntarjoajaan...');
}
}else{

$('.ajax-search-results').html($('<li/>').text("Ei hakutuloksia"));
}

},
error: function(){
alert('Error while request..');
},
complete: function() {

$("#ajax-loading-search").hide();
}
});
}
return false;
});

这是我的模型:

<?php

class Search_model extends CI_Model {

public function ajaxSearch($keyword)
{

$this->db->select('event_id, event_name, event_city')->from('events');
$this->db->like('event_name', $keyword);
$this->db->where('event_date >=', date('Y-m-d G:i:s'));
$this->db->where('visibility', 1);

$query = $this->db->get();

if($query->num_rows() >= 1) {

return $query->result();
}
}

}

最后是 Controller :

<?php 
class Ajax extends CI_Controller {

public function search() {

$this->load->model('search_model');

$search = $this->input->post('keyword');
$query = $this->search_model->ajaxSearch($search);
echo json_encode($query);
}
}

最佳答案

在 Jquery 中,您发送名称为“search”的数据

data:'search='+$("#app-search").val(),

但在模型中,您试图从“关键字”获取结果

 $search =  $this->input->post('keyword');

更改为

  $search =  $this->input->post('search');

一切都应该正常。

关于php - CodeIgniter事件记录和AJAX返回所有结果并忽略关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26581609/

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