1

I have to create a dynamic drop down menu with values from my MySQL 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 can't seem to make the cities appear in the next page.

This is from my view file which displays my drop menu this side - it 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 there's something I need to do in my controller which I don't know of.

mickmackusa
49.1k13 gold badges97 silver badges163 bronze badges
asked Mar 10, 2011 at 6:41
1

1 Answer 1

1

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

answered Mar 10, 2011 at 8:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.