开发者

php drop down list

i’m new to codeigniter and i’m working on a project. i have to create a dynamic drop down menu with values from my database, when a selection is made in the drop down as soon as you click on the submit button a new page has to occur where all the cities associated with the province selected in the drop menu appear, the cities are also in my database .My database consists of an id field, province field and a cities field.The drop menu is fine but cant seem to make the cities appear in the next page. your help will be highly appreciated ok here's my code

this is from my view file which displays my drop menu this side is ok

<?
    function writeCities($id)
    {       
        $con = mysql_connect("localhost","root","");
        if (!$con) die('Could not connect: ' . mysql_error());
        mysql_select_db("msansi", $con);
        $query  = "SELECT cities FROM provinces WHERE id =";
        $query .= $id;
        $result = mysql_query($query);          

        $row = mysql_fetch_array($result);
        echo $row[0];       
    }



    function populateDropBox()
    {
        $con = mysql_connect("localhost","root","");
        if (!$con) die('Could not connect: ' . mysql_error());
        mysql_开发者_高级运维select_db("msansi", $con);
        $result = mysql_query("SELECT id,title,cities FROM provinces");

        while($row = mysql_fetch_array($result))
        {   
            echo "<option value=$row[0]>" . $row['title']."</option>";
        }
    }
?>

<form name="myform" action="http://localhost/CodeIgniter_1.7.3/index.php/ndivhuho/submit" method="post">

    <select name = "province" onChange="onChangeDropBox();"/> 
    <? populateDropBox(); ?>    
    <input type="submit"  value="submit"; />     
    </form>

and here's my other view file which is supposed to display the cities in a text area

<?
    function writeCities($id)
    {       
        $con = mysql_connect("localhost","root","");
        if (!$con) die('Could not connect: ' . mysql_error());
        mysql_select_db("msansi", $con);
        $query  = "SELECT cities FROM provinces WHERE id =";
        $query .= $id;
        $result = mysql_query($query);          

        $row = mysql_fetch_array($result);
        echo $row[0];       
    }


?>

    <script type="text/javascript">
function onChangeDropBox()

    {
         var selected =0;
        selected = document.myform.province.value;                  

        var t = "<? writeCities(1);?>";
        document.myform.textArea.value = t;

    }
</script> 

    <form name=myform>


    <textarea name="citites" readonly="true";></textarea>
    </form>

i'm sure theres something i need to do in my controller which i don't know of thanxx in advance!!!


Take a look at the following two guides on how to do what you're talking about doing:

  • http://php-ajax-code.blogspot.com/2007/07/ajax-triple-dropdown-with-states-cities.html

  • http://roshanbh.com.np/2007/12/change-dropdown-list-options-values-from-database-with-ajax-and-php.html


There are a few problems here.

The code you have provided is using native php functions to connect to mysql. You should be using the proper CodeIgniter libraries. Start by reading this.

http://codeigniter.com/user_guide/database/examples.html

Once you've read that..

"this is from my view file which displays my drop menu"

Take the code out of your view file! The database calls should be in a model, and that should be called by a controller, which passes the data through to your view file.

Probably read this too:

http://en.wikipedia.org/wiki/Model%E2%80%93View%E2%80%93Controller

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜