1

I have a form that registers a 'property' to a chosen 'product'.

When typing the products' name - it gets it from the database using the controller, sends it to the model and gets back an array of variables.

Since jQuery 'autocomplete' works with jsons, so it echos the variables as jsons.

I am trying to get the id using a hidden form but without any success, what is the best way doing this?

Model:

public function search_prod_name($prod_name){

    $this->db->query(" SELECT prod_name, prod_id FROM prods WHERE prod_name LIKE $prod_name");
    return $this->db->get('prods')->result();

}

Controller:

// Autocomplete prod name from DB. 
public function search_prod_name(){

    if ( isset($_GET['prod_name']) ){

        $prod_name = $_GET['prod_name'];

        $result = $this->kas_model->search_prod_name();

        if (count($result) > 0){

            foreach ($result as $prod) {

                $arr_name[] = $prod->name; 
                $arr_id[]   = $prod->id;

            }

            echo json_encode($arr_name);
            echo json_encode($arr_id);
        }
    }
}

header (jQuery):

<script type="text/javascript">     
    $(document).ready(function(){
        $('#prod_name').autocomplete({
            source: "<?php echo site_url('kas/search_prod_name'); ?>"
        });
    });
</script>

Form:

<?php 

    // Give a class and ID to the 'prop_prod' autofill
    $attributes = array('class' => 'prod_name', 'id' => 'prod_name', 'name' => 'prod_name');
    $attributes2 = array('name' => 'prop_prod' );
?>

<div class="content kas_reg prop_reg">

<h1>Register a new property:</h1>

    <?php 

        echo form_open('kas/add_property');

        echo form_input('prop_word', 'property');   
        echo form_input('prod_name',  'prod Name', $attributes);    
        echo form_hidden($attributes2);
    ?>


    <select id="country" name="prop_country">
        <!-- options.. -->
    </select>


    <?php

        echo form_submit('submit', 'Submit a New property');

        echo form_close();

    ?>


</div>

I'm currently getting the names from the database, but I want to get the ID of this name. so when submitting this - I submit the ID and not the name.

EDIT:

I do have these file and I know for sure that the head is calling them. can this be a cache issue maybe?

enter image description here

1 Answer 1

1

You have to set the value and label index in the array find the definition in autocomplete api page

Controller:

// Autocomplete prod name from DB. 
public function search_prod_name(){

    $data = array();
    $prod_name = $this->input->get('term');

    if ( !empty($prod_name) ){    

        $result = $this->kas_model->search_prod_name($prod_name);

        if (count($result) > 0){

            foreach ($result as $prod) {
                $aRow = array();
                $aRow['label'] = $prod->name; 
                $aRow['value'] = $prod->name; 
                $aRow['id'] = $prod->id; 
                $data[] = $aRow;
            }
        }
    }

    echo json_encode($data);

}

Model Code

public function search_prod_name($prod_name){

    $result = $this->db->query("SELECT prod_name, prod_id FROM prods WHERE prod_name LIKE '%$prod_name%'");
    return $result->result();

}

JavaScript:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('#place_name').autocomplete({
        //minLength:2,
        source:function(request, response) {
            var sUrl = "<?php echo site_url('kas/search_prod_name'); ?>";
            $.getJSON(sUrl, request, function(result) {
                response(result);
            });
        },
        select:function(event, ui) {       
          alert('Your selected id is '+ui.item.id);
        }
    });
});
</script>
15
  • Thank you, I'll take a look at this during the day :) Feb 10, 2016 at 8:07
  • it's very simple one
    – Sundar
    Feb 10, 2016 at 8:07
  • $_GET['prod_name'] you should use this as $this->input->get('term') check the updated answer your model function error is fixed
    – Sundar
    Feb 10, 2016 at 8:10
  • #place_name or you can give your form field name
    – Sundar
    Feb 10, 2016 at 8:13
  • It's sltill auto-completing the texts from the data base, but I don't understand why it's giving me the error: Uncaught TypeError: $(...).autocomplete is not a function. for some reason I think its from the select: because I don't understand how it gets the results and in what structure, where can I console log? Feb 10, 2016 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.