Welcome Guest, Not a member yet? Register   Sign In
Dropdown filtering with Ajax in codeigniter
#1

[eluser]Unknown[/eluser]
Hi everybody, I need your help in filtering a dropdown with Ajax in codeigniter. I got a dropdown in my view which lists all product types from database. There's another dropdown which should get all attributes on the base of the product that user will choose accordingly. This is my code for controller:

function getattributetype($type)
{
$data=array();
$this->load->model('Producttypemastermodel');
header('Content-Type: application/x-json; charset=utf-8');

echo(json_encode($this->Producttypemastermodel->get_attributetype($type)));

$data['row2']=$this->Producttypemastermodel->get_attributetype($type);
$this->template->write_view('content','admin/productattr_add',$data);
$this->template->render();
}

This is the code for model:
function get_attributetype($type)
{
$query = $this->db->query("select attr_set from product_type_master where id =".$type);
$row_attr = $query->result_array();
$code = $row_attr[0]['attr_set'];

$query = $this->db->query("select * from attribute_master where id IN( ".$code.")");
$row_attr = $query->result_array();

$row2 = array();

foreach ($row_attr as $product)
{
$row2[$product['id']] = $product['title'];
}
return $row2;
//print_r($row_attr);
//echo "x";die;
}

This is the code for view:
<table border="0">
<tr>
<td> Product Type: </td>
<td>
&lt;?php
echo form_open('admin/product/getproducttype');
$options=$row1;
echo form_dropdown('product_type', $options, '', "id=type");
?&gt;
</td>
</tr>
<tr>
<td> Product Attribute Type: </td>
<td> &lt;?php
echo form_open('product_attribute/getattributetype');
//$opt=$row2;
echo form_dropdown('product_attr',$row2,'',"id=prod_attr");?&gt; </td>
</tr>

The thing's I can see the data coming in Ajax Request in console in firebug whenever i select a product in first dropdown. One More thing it shows me an error like this:


A PHP Error was encountered

Severity: Notice

Message: Undefined variable: row2

Filename: admin/productattr_add.php This is the view file in admin folder

Line Number: 38
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: helpers/form_helper.php

Line Number: 331




Theme © iAndrew 2016 - Forum software by © MyBB