Rahul Bhargava
Rahul Bhargava

Reputation: 548

jQuery UI autocomplete in codeIgniter with sql, php

enter image description here

I am trying to do create text-box auto-complete with jquery. But something goes wrong when I check it in debuger it shows the every value but it not showing in text-box drop-down on view page. I'm newbie all help would be appreciated.

here's my view code.

<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>

<script type="text/javascript">
    $(function(){
        $("#location").autocomplete({
            source: 'set_location'
        });     
    });
</script>
<form action= "#">
    <table>
        <tr>
            <td><label for='location'>Location</label></td>
            <td><input id="location" type="text" size="50" placeholder="Enter a location" name= "location"> </td>
        </tr>           
    </table>
</form>

here's my controller code

if(isset($_GET['term'])){
    $location = strtolower($_GET['term']);      
    $query = $this->venues_model->set_location($location);          
    echo $query;
}

here's my model code

$this->db->select('*');
$this->db->like('location', $location);
$query = $this->db->get('venue_details');

if(count($query->result_array()) > 0){ 
    foreach ($query->result_array() as $row){
        $row_set[] = htmlentities(stripslashes($row['location']));
    }
    echo json_encode($row_set);
}

Upvotes: 0

Views: 457

Answers (3)

akinlex
akinlex

Reputation: 23

just add ui-widget to your div. Make sure to reference jquery ui

Upvotes: 0

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

try this,

if(isset($_GET['term'])){
$location = strtolower($_GET['term']);      
echo $this->venues_model->set_location($location);          
}

in model

function set_location() {
    $row_set = array();
    $this->db->select('*');
    $this->db->like('location', $location);
    $query = $this->db->get('venue_details');

    if (count($query->result_array()) > 0) {
        foreach ($query->result_array() as $row) {
            $row_set[] = htmlentities(stripslashes($row['location']));
        }

    }

    return json_encode($row_set);
}

also this,

source: '<?= base_url("controller/set_location") ?>'

Upvotes: 1

Saty
Saty

Reputation: 22532

You have to return from your model file and echo from your controller

Model

$this->db->select('*');
$this->db->like('location', $location);
$query = $this->db->get('venue_details');

if(count($query->result_array()) > 0){ 
    foreach ($query->result_array() as $row){
        $row_set[] = htmlentities(stripslashes($row['location']));
    }
    return json_encode($row_set);// return from model
}

Controller

if(isset($_GET['term'])){
    $location = strtolower($_GET['term']);      
   echo  $query = $this->venues_model->set_location($location);          
}

And change your path to

source: '<?php echo base_url();?>index.php/controller/function'

Upvotes: 0

Related Questions