简体   繁体   中英

codeigniter not returning data from database based on where condition

i am trying to get get some data through an ajax call in codeigniter based on the user input but i keep getting all result from the database instead of the matching ones.

here is my ajax:

$(document).ready(function () {
    $(".filter").keyup(function () {
        var keyword= $("#loc").val();
        $.ajax({
            type: "POST",
            url: CFG.url + '/pages/filter_hotel_by_loc/',
            data: keyword,
            dataType: "json",
            success: function (data) {
                console.log(data);
            }
        });
});
});

Model:

    public function hotel_by_location($keyword){
        $this->db->like("location", $keyword);
        $query = $this->db->get('hotels');
        return $query->result_array();

Controller:

    public function filter_hotel_by_loc(){
    $keyword=$this->input->post('loc');
    $data=$this->page_model->hotel_by_location($keyword);        
    echo json_encode($data);

View:

<div class="input-style-1 b-50 color-3">
<?php echo form_open_multipart('hotels/filter_hotel_by_loc') ?>
<img src="<?php echo base_url();?>assets/img/loc_icon_small_grey.png"alt="">
<input type="text" name="loc" placeholder="What city do you prefer?"id="loca" class="filter">
</form>
</div>

Please what am i doing wrong?

your input field of keyword have id loca but you used loc in keyword selector and post data must be in json format,so your ajax code should be like that

$(document).ready(function () {
 $(".filter").keyup(function () {
    var keyword= $("#loca").val();
      $.ajax({
          type: "POST",
          url: CFG.url + '/pages/filter_hotel_by_loc/',
          data: {'keyword':keyword},
          dataType: "json",
          success: function (data) {
             console.log(data);
          }
       }); 
    });
});

and replace this line

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

with this

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

and must check value of keyword which you get in post before pass it to model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM