Search code examples
phpjqueryjquery-uiautocompletejquery-ui-autocomplete

JQuery UI Autocomplete Multiple(Multiple Values for Multiple Input)


I cant find the answer on the internet.

This is my input box :

<div class="form-group">
    <div id="prefetch">
        <label for="external_name" class="font-weight-bold">Choose External File as Reference</label>
        <input type="text" name="external_name" id="external_name" class="form-control" placeholder="External File"/>
        <input type="hidden" name="external_id" id="external_id" class="form-control" value='-'/>
        <small>*Previous value : <?php if(empty($sedata->external_name) == FALSE){echo $sedata->external_name;}else{echo "-";} ?></small>
    </div>
</div>

And this is my jquery code :

         $("#external_name").autocomplete({
            source: function (request, response) {
                // Fetch data
                $.ajax({
                    url: "<?php echo base_url('Se/acexternalrefse') ?>",
                    type: 'post',
                    dataType: "json",
                    data: {
                        search: request.term
                    },
                    success: function (data) {
                        response(data);
                    }
                });
            },
            select: function (event, ui) {
                // Set selection
                $('#external_name').val(ui.item.label); // display the selected text
                $('#external_id').val(ui.item.value); // save selected id to input
                return false;
            }
        });

And this is the data from model and controller :

public function ac_externalref_se($postData){
        $response = array();

        if(isset($postData['search']) ){
            $records = $this->db->select('*')->order_by('external_name','ASC')->like('external_name', $postData['search'],'both')->limit(10)->get('external')->result();
            foreach($records as $row ){
                $response[] = array("value"=>$row->external_id,"label"=>$row->external_name);
            }
        }

        return $response;
    }

        public function acexternalrefse(){
        $postData   =   $this->input->post();
        $data       =   $this->SE_model->ac_externalref_se($postData);

        echo json_encode($data);
    }

The output will be allowing you to choose 1 value, not multiple for input text external_name and input hidden external_id

I don't understand how to get multiple values for multiple inputs like:

value of input text external name = PHP, Java, JavaScript

value of input text external id = 1, 5, 9

if you have the experience to do a multi-select multi-input jquery UI Autocomplete please share with me how to do it, I'm new to js, jquery,jquery-UI....


Solution

  • nvm, ive find the answer on : https://makitweb.com/jquery-ui-autocomplete-with-php-and-ajax/

    and adding

    focus: function() {
       return false;
    },