I have "input1" that looks for the name of a client and saves its id in the table, this search is done by autocomplete with JavaScript.
Now you need to have the "partner" id linked to the client id that has been 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 its 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 direct partner id insertion in the DB, I just can not get 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);
}
}
In the image, the input stores the id = 7 corresponding to the name Bridgestone, your partner would be id = 2
Everything is working, I can make the registrations and display them, I just can not save the partner id linked to the client id I've already taken.