0

i am working in a code igniter .. i am making five rows ..actually i am doing is that the options which are display in the 2nd select box based on the first select box .. if i dont make five rows with the loop then script is working fine but if i put them in a loop ..selection dont work .. in firebug its give me response false and saying that

  localhost/......./controller/get_items/undefined...

i dont know whats wrong in that code

<?php 
for ($i = 0; $i < 5; $i++) {
?>
<?php echo form_dropdown('cat_id', $records2, '#', "id='category_".$i."'");?>

<?php echo form_dropdown('item_id', $records3, '#', "id='items_".$i."'"); ?>

<script type="text/javascript">
// <![CDATA[
$(document).ready(function()
{  
    for (var i= 0; i<5; i++)
    {
        $('#category_'+ i).change(function()
        {
            $('#items_'+ i > option").remove(); 
            var category_id = $('#category_'+ i).val(); 

            $.ajax({
                type    : "POST",
                url     : "stockInController/get_Items/"+category_id, 
                success : function(items)
                {
                    $.each(items,function(item_id,item_name) 
                    {
                        var opt = $('<option />'); 
                        opt.val(item_id);
                        opt.text(item_name);
                        $('#items_'+ i).append(opt); 
                    });
                }

            });
        });
    }
}      
Muhammad Raheel
  • 19,823
  • 7
  • 67
  • 103
user1972143
  • 89
  • 2
  • 10
  • 20
  • It means `$('#category_'+i)` hasn't got a value – andy Jan 18 '13 at 13:29
  • @andy why it is not getting a value ..i think i am doing all right .. see my updated question – user1972143 Jan 18 '13 at 13:33
  • your script is totally wrong because you are initializing function in a loop – Muhammad Raheel Jan 18 '13 at 13:40
  • what is this ` $('#items_'+ i > option").remove(); ` quotations alert!! – mamdouh alramadan Jan 18 '13 at 13:41
  • @raheel shan .. so what should i do ? please can u rearrange it or so i get to know how to make it right – user1972143 Jan 18 '13 at 13:41
  • tell me clearly what you want step by step – Muhammad Raheel Jan 18 '13 at 13:42
  • @raheel shah i have a form where i display 5 rows in a loop n in each row there r 2 dropdown fields n three simple text input boxes so when i was working in simple one simple i mean when the form was not in a loop then the two dropdown boxes working fine which is what that "the second dropdown options based on the selection of previous dropdown select box" so then i put them in a loop..and attach "$i" with the id of every select box because i have to given each of the row a different ids because if i dont do so then only the select boxes of first row is successfully working not other four ? – user1972143 Jan 18 '13 at 13:54
  • so then as i was getting data from controller through jquery ... so i have to given different ids in jquery also in order for jquery to catch the values and send to controller and then give back the response ..which i did ..did u get my problem ? – user1972143 Jan 18 '13 at 13:57
  • what is the use of textboxes – Muhammad Raheel Jan 18 '13 at 13:59
  • Unless your doing XHTML you can get rid of the cdata wrapper.http://stackoverflow.com/questions/66837/when-is-a-cdata-section-necessary-within-a-script-tag – jholloman Jan 18 '13 at 13:59
  • @raheel shah actually it is like that categories ->products->price->quantity->total – user1972143 Jan 18 '13 at 14:01
  • so it means on category selection you are fetching products then on products selection you are selecting price , on price you are selecting quantity etc.. – Muhammad Raheel Jan 18 '13 at 14:02
  • @raheel no no ... i am just fetching products based on the categories ... the price and other things are for the user .. he manually type it – user1972143 Jan 18 '13 at 14:09
  • `for ($i = 0; $i < 5; $i++) {` where this `for` ends? I don't see the `` – Tomás Jan 18 '13 at 14:50
  • i have ended the php tag ..i just didnt write here.. – user1972143 Jan 18 '13 at 16:38

1 Answers1

0

As far as i understand your problem here is the solution.

Yor function should look like this

function getCategories(obj)
{

    var category_id = obj.id      ;
    $.ajax({
         type    : "POST",
         url     : "stockInController/get_Items/"+category_id, 
         success : function(data)
         {
             $('#'+id).html(data);// here you should provide 2nd dropdown id so that it can be replaced
         }
    });
}

Controller method

function get_Items()
{
    $id =   $this->uri->segment(3);
    $data['result'] = $this->somemodel->getProducts($id);
        $this->load->view('options',$data);
}

View

options.php

foreach($result as $row){
?> <option value = "<?php echo $row->item_id?>"><?php echo $row->item_name?></option><?php    
}

And on each dropdown of categories you should call this function.

<?php echo form_dropdown('cat_id', $records2, '#', "id='category_".$i."' onclick = "getCategories(this)"");?>
Muhammad Raheel
  • 19,823
  • 7
  • 67
  • 103