Welcome Guest, Not a member yet? Register   Sign In
Dynamic Dependent List Boxes
#1

[eluser]Unknown[/eluser]
hi i have the same problem with one of the member here. his screen name is theighost.

Here's the issue of theighost and my issue now as well:

"I am using codeigniter for a site and need to make a script that makes the following :

I have a dropdown list named “states” for example,and another drop down list named “cities”.

I want when i select a state the “cities” dropdown list to refresh automaticly,showing the cities in the state selected.

I saw many scripts on the net…but they were not for codeiginter and they were using the method get, which i dont really know how to apply it in codeigniter.

BTW the states and the cities are taken from a database and are not static info.

any help would be appreciated."

--------------------- My Code --------------------------------
MY CONTROLLER

$data['con_type'] = $this->db->query('SELECT type FROM contact_type');

MY VIEW PAGE

[removed]
function selType(Type)
{
var type = Type[Type.selectedIndex].value;
setList(type);
}
function callToSetList(data)
{
$('#list').html(data);
}
function setList(type)
{
$.post("ff.php", {ctype: type}, callToSetList);
}
[removed]

<select name="c_type">
&lt;? foreach($con_type->result() as $row): ?&gt;
<option>&lt;?=$row->type?&gt;</option>
&lt;? endforeach; ?&gt;
</select> <br /><br />

<select id="list" size="25" style="width:240px;">
&lt;? foreach($list->result() as $row): ?&gt;
<option>&lt;?=$row->name?&gt;</option>
&lt;? endforeach; ?&gt;
</select> <br />

FF.PHP page(I placed this outside system folder which is (base_url))

&lt;?
mysql_connect("localhost", "root", "");
mysql_select_db("contacts");

$ctype = $_POST['ctype'];

$result = mysql_query("SELECT * FROM contacts WHERE type='$ctype'");

while($row = mysql_fetch_array($result))
{ ?&gt;
<option value="&lt;?=$row['name']?&gt;">&lt;?=$row['name']?&gt;</option>
&lt;? } ?&gt;

------------------------------------------------------------------
My problem with codeigniter is that we cannot post($_POST) anything in the view page just like in the normal PHP - in codeigniter which we can only do in the controller or model, so our only option would be to create a javascript.

anyone here who has a better algorithm for this issue?

thanks!
#2

[eluser]rakesh[/eluser]
use some ajax library like jquery in view file to send the variable to the controller wich in return give the required dynamic list
#3

[eluser]Unknown[/eluser]
thanks for giving some time to read and reply on my post.

can you please give me an example on how to do that pass an ajax to the controller.

thanks a lot




Theme © iAndrew 2016 - Forum software by © MyBB