我的codeigniter项目中的ajax搜索有问题。 搜索没有显示数据,点击SEARCH按钮页面后冻结1-2秒。我认为问题在于从数据库中获取信息,但模型也是正确的。
有代码:
型号:
var $table = 'users';
function search($options = array(), $status = true, $limit = null, $offset = 0) {
$this->db->select('users.username,types.t_title,media.photo');
$str = '';
if ($options['username'] != '') {
$str .= ' AND users.username =' . $this->db->escape($options['username']);
}
if (!$status) {
$str .= ' AND users.active = 1';
}
$this->db->where('users.username', 'LIKE', '%' . $options['username'] . '%' . $str);
$this->db->group_by('users.id');
$this->db->limit($limit, $offset);
$search = $this->db->get($this->table);
return $search->result();
}
控制器:
public function search($offset = 0) {
$options = $this->Users->array_from_post(array('username'));
if ($this->form_validation->run('search')) {
$count = $this->Property->search($options, $status = FALSE);
$perpage = 10;
if (count($count) > $perpage) {
$config['base_url'] = site_url('welcome/search');
$config['total_rows'] = count($count);
$config['per_page'] = $perpage;
$config['uri_segment'] = 4;
$q = $this->pagination->initialize($config);
$offset = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
} else {
$this->data['pagination'] = '';
$offset = 0;
}
$status = 1;
$this->data['search'] = $this->Users->search($options, $status = FALSE, $perpage, $offset);
$this->load_search('search');
}
}
用ajax查看:
<script type="text/javascript">
$(document).ready(function() {
$("form[name='search']").submit(function(e) {
var formData = new FormData($(this)[0]);
$.ajax({
url: "<?php echo site_url('welcome/welcome/search'); ?>",
type: "POST",
dataType: "text",
data: formData,
async: false,
success: function (msg) {
$(".search_area").html(msg);
},
cache: false,
contentType: false,
processData: false
});
e.preventDefault();
});
});
</script>
<form class="form-horizontal" method="post" name="search">
<div class="row">
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-6">
<div class="form-group">
<div class="col-md-12">
<input type="text" class="input-text" id="username" name="username" placeholder="Username">
</div>
</div>
</div>
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-6">
<div class="form-group">
<input type="submit" class="search-button" name="search" value="Search">
</div>
</div>
</div>
</form>
<div class="search_area">
</div>
全部已连接并加载模板,但唯一的消息为No Record Found
这是搜索视图:
<div class="main-title">
<h1>Search Results</h1>
<div class="border">
<div class="border-inner"></div>
</div>
</div>
<?php if ($search) : ?>
<?php foreach ($search as $sr): ?>
<p>Tralalala <?php echo $sr['title']; ?></p>
<?php endforeach; ?>
<?php else:?>
<div class="notification-box">
<h3><?php echo $this->lang->line('no_record'); ?></h3>
</div>
<?php endif; ?>