Welcome Guest, Not a member yet? Register   Sign In
Update a Select with onchange of other Select
#1

[eluser]Nuno Simões[/eluser]
I there, I'm a newbie on CI and I'm having a problem:

On my view page nammed init.php, I have two select boxes on a form and I want to update one select (modelo) with data from database after change selection on other select (fabricante). Does anyone tell me the better way to do this?

Thanks a lot!

There's the Code:

Code:
<form name="detailsearch" action="#" method="get">
    <input name="search_statement" type="text" /><br />
        <div id="label">Fabricante: </div>
        <select name="fabricante">
            <option value="0">Indiferente</option>
            &lt;?php
        $i=0;
                foreach($fabricantes->result() as $row) {
            echo "<option value=\"".++$i."\">".$row->NOME."</option>\n";
        }    
            ?&gt;
        </select><br />
        <div id="label">Modelo: </div>
        <select name="modelo">
            <option value="0">Indiferente</option>
        </select><br />
&lt;/form&gt;
#2

[eluser]abmcr[/eluser]
i think do you nedd use an ajax wrapper..... after select in the first select box, you call the ajax function for populate the second select
#3

[eluser]Nuno Simões[/eluser]
Thanks for the answer abmcr.

I've already worked with the SACK, but I don't know how to integrate this with CodeIgniter. I'm searching right now for a good tutorial 'bout jQuery and CodeIgniter, one that explains the things, not the ones that teaches you to do a thing and nothing more. If you know some, report it to me, OK?

Thanks a lot
#4

[eluser]abmcr[/eluser]
The php code in a view
Code:
$js = 'onChange="get_dati_gerachia()"';
echo form_dropdown('dati_gerarchia', $options, "TRUE",$js);echo "\n";
.....
    $id = 'id="master" ';
            $options = array(
                              'y'  => 'Master',
                              'n'  => 'Normale'
                            );
            echo "<span id='ajax_target'>".form_dropdown('master', $options,'',$id)."</span>";
and the js routine
Code:
function get_dati_gerachia(){
    var chosenItemText = $("select[name='dati_gerarchia']").val();
     a="data="+chosenItemText;
     $.ajax({
      type: "POST",
      url:  "&lt;?php echo site_url("the_url_of_your_ajax_controller")?&gt;",
      data: a,
      success: function(msg){
        $("#ajax_target").html(msg);
        }
    });
}
and your ajax controller
Code:
function the_url_of_your_ajax_controller(){
        $var=$this->input->post("data");
..... some operations and build your select dependency
$html="<select......."
echo $html;
}
Into this the_url_of_your_ajax_controller you build the second select and echo to the ajax_target div
#5

[eluser]Nuno Simões[/eluser]
Thanks, I had some problems getting the selected value, but is solved now.

I have another question, what do I need to load to make the next piece of code to work:

Code:
$.ajax({
      type: "POST",
      url:  "&lt;?php echo site_url("the_url_of_your_ajax_controller")?&gt;",
      data: a,
      success: function(msg){
        $("#ajax_target").html(msg);
        }
    });
#6

[eluser]abmcr[/eluser]
Oooops...... i forget!
In this sample i have used jquery librarie.... only the core
download at www.jquery.com
Ciao!
#7

[eluser]Nuno Simões[/eluser]
It worked! Big Grin Thanks a lot.

If you come to Portugal, just let me know and I pay you a beer! Wink




Theme © iAndrew 2016 - Forum software by © MyBB