Fill input2 from input1?

Asked

Viewed 65 times

0

I have the "input1" that searches the name of a client and saves his id in the table, this search is done by autocomplete with Javascript.

Now I needed that in "input2" I received the id of the "partner" linked to the id of the client that was inserted in "input1", both belong to the same table (client and partner).

It is not necessary to show the partner name in the form, just save your id corresponding to the client id that is already displayed.

The display of the table with the client and partner column is OK, I tested with insertion of the direct partner id in the BD, I just can not through the form.

<div class="control-group">
 <label for="clientes" class="control-label">Cliente</label>
   <div class="controls">
      <input id="cliente"  type="text" name="cliente"  />
      <input id="clientes_id" type="hidden" name="clientes_id" value=""/>
   </div>
</div>

<script type="text/javascript">
$(document).ready(function(){
    $("#cliente").autocomplete({
        source: "<?php echo base_url(); ?>index.php/exportacao/autoCompleteCliente",
        minLength: 1,
        select: function( event, ui ) {
             $("#clientes_id").val(ui.item.id);
        }
  });    
}

Controller: Export.php

public function autoCompleteCliente(){
    if (isset($_GET['term'])){ //devolve indice referente ao nome do cliente
        $q = strtolower($_GET['term']);
        $this->fatexpo_model->autoCompleteCliente($q);
    }        
}

model: fatexpo_model.php

 public function autoCompleteCliente($q){
     $this->db->select('*');
     $this->db->limit(5);
     $this->db->like('nomeCliente', $q);//busca pelo nome do cliente
     $query = $this->db->get('clientes');
     if($query->num_rows() > 0){
         foreach ($query->result_array() as $row){
             $row_set[] = array('label'=>$row['nomeCliente'],'id'=>$row['idClientes']);
         }
         echo json_encode($row_set);
     }
 }

Autocomplete do form

In the image, the input stores the id = 7 corresponding to the name Bridgestone, your partner would id = 2

Everything is working, I can register and display them, I just can’t save the partner id linked to the client id I already got.

  • 1

    Your question is confused, let’s go in parts: you have several clients in the database (which is what is being searched for by autocomplete) and each of these clients has a field parceiroId, right?

  • Hello, sorry, I couldn’t be more specific. This, I have a table "clients": idClientes, client name and partners_id. The partner_id stores the id of another table "partners": idPartners, namePartner and cnpj. My form stores the data in a third table "export". Thanks for the willingness to help.

  • Partner id already comes in autocomplete?

  • All right, @Matheusmadalozzo That said, let’s go to the next step: what you want to do is: by selecting a client which shows, in addition to the name, in the field below the value of parceiros_id?

  • @LINQ this, I needed to take the value of partners_id to save it in my third table "export" in "partner_expo". The customer id I already store in this export table.

  • @Dvd no friend, only the idClientes.

  • @Matheusmadalozzo See my answer.

  • @LINQ excellent, I will try and get back to you as soon as possible.

  • 2

    @LINQ case solved! Excellent help, thank you very much for the solution, availability and agility!

Show 4 more comments

1 answer

0


The first thing you need to do is adapt the function autoCompleteCliente to return - in addition to idClientes and nomeCliente - the field parceiros_id.

public function autoCompleteCliente($q){
    $this->db->select('*');
    $this->db->limit(5);
    $this->db->like('nomeCliente', $q);//busca pelo nome do cliente
    $query = $this->db->get('clientes');
    if($query->num_rows() > 0){
        foreach ($query->result_array() as $row) {
            $row_set[] = array('label' => $row['nomeCliente'], 
                               'id' => $row['idClientes'], 
                               'parceiroId' => $row['parceiros_id']);
        }
        echo json_encode($row_set);
    }
}

Then, it is necessary to adapt the Javascript function which is triggered as soon as an element of the autocomplete is selected, causing the value received in ui.item.parceiroId be inserted into some input. Since you did not specify this input in the question, I left as an example the selector #parceiro_id.

$(document).ready(function(){
    $("#cliente").autocomplete({
        source: "<?php echo base_url(); ?>index.php/exportacao/autoCompleteCliente",
        minLength: 1,
        select: function( event, ui ) {
            $("#clientes_id").val(ui.item.id);
            $("#parceiro_id").val(ui.item.parceiroId);
        }
    });    
}

With these modifications, you will be able to see the value of the field parceiros_id in the input.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.