Reputation: 225
In my view, I have a jquery which sends a select dropdown value to my controller, and then I call a function in my model to run a query using this value and get a result array from the database that will be displayed on a table in my view.
Here is the jquery section on my view
$('#idFamilia').change(function(){
var clave = $("#idFamilia option:selected").text();
if (clave != "Seleccione"){
$.ajax({
url: '<?php echo base_url(); ?>index.php/Proveedor/obtenerListaProveedorFamilia',
method: 'POST',
data: {
clave: clave
},
success: function (returned) {
console.log(returned);
//Here I need to get the array listaproveedorfamilias
}
});
}
});
Here is the code for my controller section where I post the value and call the model function
function obtenerListaProveedorFamilia(){
$this->load->model('Proveedormodel');
$clave = $_POST['clave'];
$data['listaproveedorfamilia'] = $this->Proveedormodel->get_all_listaproveedorfamilia($clave);
$data['_view'] = 'proveedor/index';
$this->load->view('layouts/main',$data);
}
Here is the function that runs the query in my model. I already verified it works using the console log in the jquery function
function get_all_listaproveedorfamilia($clave)
{
$this->db->select('proveedor.razonSocial, proveedor.nombre1, proveedor.telefonoFijo1, proveedor.telefonoMovil1, proveedor.correoElectronico1, proveedor.tipo, familia.clave');
$this->db->from('proveedor');
$this->db->join('relacionproveedorfamilia', 'relacionproveedorfamilia.idProveedor = proveedor.id', 'inner');
$this->db->join('familia', 'familia.id = relacionproveedorfamilia.idFamilia', 'inner');
$this->db->where('familia.clave', $clave);
$this->db->order_by('proveedor.razonSocial');
$query = $this->db->get();
if($query->num_rows() > 0){
return $query->result_array();
}
}
The problem is I can't seem to use the array $data['listaproveedorfamilia'] in my view. This are some things that I have already tried:
-Loop through the array and get each value, this would be appended as a table in my view. (This is just the section where I use the array values)
"<?php foreach($listaproveedorfamilia as $p){ ?>" +
"<tr>" +
"<td><?php echo $p['razonSocial']; ?></td>" +
"<td><?php echo $p['nombre1']; ?></td>" +
"<td><?php echo $p['telefonoFijo1']; ?></td>" +
"<td><?php echo $p['telefonoMovil1']; ?></td>" +
"<td><?php echo $p['correoElectronico1']; ?></td>" +
"<td><?php echo $p['tipo']; ?></td>" +
"<td><?php echo $p['clave']; ?></td>" +
"</tr>"
"<?php } ?>"
-Save the array in a javascript array variable
var myArray = new Array();
myArray = <?php echo json_encode($listaproveedorfamilia); ?>
None of these are working. Everytime I load the view in the browser I see the following error message in the console.
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: listaproveedorfamilia
If I understand correctly, the problem is that as soon as the page loads, it's trying to find the variable listaproveedorfamilia. The problem is that first the page needs to load, then the user needs to select an option from the dropdown, after that the selection is sent to run a query and the result should be saved in the variable.
How could I make this work? Thanks for your help.
Upvotes: 1
Views: 1923
Reputation: 1646
You should modify your function obtenerListaProveedorFamilia()
as following:
function obtenerListaProveedorFamilia(){
$this->load->model('Proveedormodel');
$clave = $_POST['clave'];
$data['listaproveedorfamilia'] = $this->Proveedormodel->get_all_listaproveedorfamilia($clave);
echo json_encode($data);
}
And in your get data in ajax function and JSON.parse
it then to get all value use jQuery.each()
function to get your expected array value.
$('#idFamilia').change(function(){
var clave = $("#idFamilia option:selected").text();
if (clave != "Seleccione"){
$.ajax({
url: '<?php echo base_url(); ?>index.php/Proveedor/obtenerListaProveedorFamilia',
method: 'POST',
data: {
clave: clave
},
success: function (returned) {
var returned = JSON.parse(returned);
jQuery.each( returned.listaproveedorfamilia, function( i, val ) {
alert('razonSocial= '+ val.razonSocial + 'nombre1'+ val.nombre1 );
});
console.log(returned);
//Here I need to get the array listaproveedorfamilias
}
});
}
});
Upvotes: 2